100分求教浙大1118代码

luoqiutang 2009-12-06 10:29:37
Background

An n-tersection is defined as a location in n-dimensional space, n being a positive integer, having all non-negative integer coordinates. For example, the location (1,2,3) represents an n-tersection in three dimensional space. Two n-tersections are said to be adjacent if they have the same number of dimensions and their coordinates differ by exactly 1 in a single dimension only. For example, (1,2,3) is adjacent to (0,2,3) and (2,2,3) and (1,2,4), but not to (2,3,3) or (3,2,3) or (1,2). An n-teresting space is defined as a collection of paths between adjacent n-tersections.

Finally, an n-credible maze is defined as an n-teresting space combined with two specific n-tersections in that space, one of which is identified as the starting n-tersection and the other as the ending n-tersection.


Input

The input file will consist of the descriptions of one or more n-credible mazes. The first line of the description will specify n, the dimension of the n-teresting space. (For this problem, n will not exceed 10, and all coordinate values will be less than 10.) The next line will contain 2n non-negative integers, the first n of which describe the starting n-tersection, least dimension first, and the next n of which describe the ending n-tersection. Next will be a nonnegative number of lines containing 2n non-negative integers each, identifying paths between adjacent n-tersections in the n-teresting space. The list is terminated by a line containing only the value �C1. Several such maze descriptions may be present in the file. The end of the input is signalled by space dimension of zero. No further data will follow this terminating zero.


Output

For each maze output it��s position in the input; e.g. the first maze is ��Maze #1��, the second is ��Maze #2��, etc. If it is possible to travel through the n-credible maze��s n-teresting space from the starting n-tersection to the ending n-tersection, also output ��can be travelled�� on the same line. If such travel is not possible, output ��cannot be travelled�� instead.


Example

Input



2
0 0 2 2
0 0 0 1
0 1 0 2
0 2 1 2
1 2 2 2
-1
3
1 1 1 1 2 3
1 1 2 1 1 3
1 1 3 1 2 3
1 1 1 1 1 0
1 1 0 1 0 0
1 0 0 0 0 0
-1
0
Output

Maze #1 can be travelled
Maze #2 cannot be travelled

...全文
412 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoqiutang 2009-12-10
  • 打赏
  • 举报
回复
继续顶..求教啊
luoqiutang 2009-12-10
  • 打赏
  • 举报
回复
楼上贴错了


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxint = 0x7fffffff;
const long long max64 = 0x7fffffffffffffffll;
const int N = 10;
const int M = 1024;
int n;
int node[M][10],top,src[10],dest[10],a[10],b[10],vis[M];
int g[N][N],isrc,idest;

bool equal(int a[10],int b[10])
{
for(int i = 0;i < n;i++) {
if(a[i] != b[i]) return false;
}
return true;
}

int push(int a[10])
{
int i;
for(i = 0;i < top;i++) {
if(equal(a,node[i]))
return i;
}
for(i = 0;i < n;i++) {
node[top][i] = a[i];
}
return top ++;
}

bool dfs(int u)
{
//printf("dfs %d\n",u);
if(u == idest) return true;
vis[u] = 1;
for(int i = 0;i < top;i++) {
if(g[u][i] && !vis[i]) {
if(dfs(i)) {
return true;
}
}
}
return false;
}

bool search()
{
int i,j,k;
isrc = -1,idest = -1;
for(i = 0;i < top;i++) {
if(equal(node[i],src)) isrc = i;
if(equal(node[i],dest)) idest = i;
}
if(isrc == -1 || idest == -1) return false;
//printf("isrc=%d,idest=%d\n",isrc,idest);
memset(vis,0,sizeof(vis));
vis[isrc] = 1;
return dfs(isrc);
}

int main()
{
int i,j,k,ia,ib,testid = 1;
while(scanf("%d",&n) && n) {
for(i = 0;i < n;i++) { scanf("%d",src + i); }
for(i = 0;i < n;i++) { scanf("%d",dest + i); }
top = 0;
memset(g,0,sizeof(g));
while(1) {
scanf("%d",&k);
if(-1 == k) break;
a[0] = k;
for(i = 1;i < n;i++) { scanf("%d",a + i);}
for(i = 0;i < n;i++) { scanf("%d",b + i);}
ia = push(a);
ib = push(b);
g[ia][ib] = 1;
g[ib][ia] = 1;
}
/*
for(i = 0;i < top;i++) {
for(j = 0;j < top;j++) {
printf("%2d",g[i][j]);
}
putchar(10);
}
putchar(10);
*/
if(search()) {
printf("Maze #%d can be travelled\n",testid++);
}else {
printf("Maze #%d cannot be travelled\n",testid++);
}
}
return 0;
}


luoqiutang 2009-12-10
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
int main()
{
int num[15];
int n;
int i,j;
int sum;

while (1)
{
i=0;
cin>>n;
sum=0;
if (n==-1)
{
break;
}
while (1)
{
num[i]=n;
i++;
cin>>n;
if (n==0)
{
break;
}
}
n=i;
for (i=0;i<n;i++)
{
for (j=i+1;j<n;j++)
{
if (num[j]*2==num[i] || num[i]*2==num[j])
{
sum++;
}
}
}
cout<<sum<<endl;

}
return 0;
}



可对否?
Owenini 2009-12-09
  • 打赏
  • 举报
回复
这是什么????????
luoqiutang 2009-12-09
  • 打赏
  • 举报
回复
顶起来...
luoqiutang 2009-12-08
  • 打赏
  • 举报
回复
没学过运筹学...我这acm全部课程才10次,啥都没学过.....
perfecttt 2009-12-08
  • 打赏
  • 举报
回复
貌似运筹学中的图论。图论中有讲怎么把一个有权值的图(迷宫简单点,无权值就行)存在一个二维的数组了(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10)中,然后就是怎么求从一个地点到另一个地点最短路最优化的问题。解迷宫就可以用到了!
潇湘呆子 2009-12-08
  • 打赏
  • 举报
回复
楼上有强人,
我自一边闪!

不懂,帮你顶!
luoqiutang 2009-12-08
  • 打赏
  • 举报
回复
顶起来...
jianzhibeihang 2009-12-07
  • 打赏
  • 举报
回复
不会 帮顶了
绿色夹克衫 2009-12-07
  • 打赏
  • 举报
回复
不知道对题目理解对了没有(自我感觉有问题),

0 0 2 2 从0,0 走到 2,2

0 0 0 1
0 1 0 2
0 2 1 2
1 2 2 2

00-01-02-12-22

不过感觉没有用上 Two n-tersections are said to be adjacent if they have the same number of dimensions and their coordinates differ by exactly 1 in a single dimension only

这部分,临近似乎没什么用呀!

如果是我理解的意思的话,有点太简单了,把字符串从中间一分为二,一个是起点的名字,
一个是终点的名字,建立好联系之后,用dfs或bfs都可以,如果超时的话就用双向搜,应该比较简单。
tigermfh 2009-12-07
  • 打赏
  • 举报
回复
这是一个走迷宫的问题而且是多维空间的,如果你做过二维数组的走迷宫那么这个问题就会有思路了。
luoqiutang 2009-12-07
  • 打赏
  • 举报
回复
谢谢3楼...我连2维的都没做过...
huangbo0603 2009-12-06
  • 打赏
  • 举报
回复
只能帮顶了!
luoqiutang 2009-12-06
  • 打赏
  • 举报
回复
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1118
这是浙大的链接,谢谢热心人.

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧