CSU 1080: A simple maze
来源:互联网 发布:javascript项目实战 编辑:程序博客网 时间:2024/05/21 09:35
典型的bfs。
代码如下:
#define MAXN 55
#define INF 0x7fffffff
#include<stdio.h>
#include<string.h>
int T, N, M, Sr,Sc,Er,Ec, min;
char s[MAXN][MAXN], vis[MAXN][MAXN];
int q[MAXN*MAXN], dis[MAXN][MAXN];
int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0};
void solve(int x, int y)
{
min = INF;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
int front = 0, rear = 0;
int u = (x-1)*M + y;
vis[x][y] = 1; dis[x][y] = 0;
q[rear++] = u;
while(front < rear)
{
u = q[front++];//printf("...u==%d\n",u);
y = u%M;
if(y==0) y = M;
x = (u-y)/M + 1;// printf("...x=%d y=%d\n",x,y);
for(int i = 0; i < 4; i ++)
{
int nx = x+dx[i], ny = y+dy[i];
if(nx>0 && nx<N+1 && ny>0 && ny<M+1 )
{
if('1'-s[nx][ny] && !vis[nx][ny])
{
int v = (nx-1)*M + ny;//printf("v==%d\n",v);
q[rear++] = v;
vis[nx][ny] = 1;
dis[nx][ny] = dis[x][y] + 1;
}
}
if(u == (Er-1)*M+Ec && dis[x][y] < min) min = dis[x][y];
}
}
if(min == INF)printf("No Path.\n");
else printf("%d\n",min);
}
void input()
{
while(scanf("%d",&T) == 1)
while(T --)
{
scanf("%d%d",&N, &M);
scanf("%d%d%d%d",&Sr,&Sc,&Er,&Ec);
for(int i = 1; i <= N; i ++)
scanf("%s",s[i]+1);
solve(Sr,Sc);
}
}
int main()
{
input();
return 0;
}
0 0
- CSU 1080: A simple maze
- A simple maze function
- CSU 1566The Maze Makers
- CSU 1566The Maze Makers
- A. Maze
- A - A Dangerous Maze
- Simple Line Editor----CSU 1019
- Water --- CSU 1550: Simple String
- CSU---D: Simple Line Editor
- codeforces 377A Maze
- CodeForces 377 A. Maze
- Codeforces 377A Maze
- CodeForces 377A - maze
- Rat in a Maze
- Maze CodeForces - 377A
- LightOJ1027-A Dangerous Maze
- A Dangerous Maze ( 期望 )
- CodeForces 377A Maze
- UVA 825 - Walking on the Safe Side(重做)
- uva 10069 - Distinct Subsequences
- HDU ACM 1292 "下沙野骆驼"ACM夏令营->DP
- POJ 2785 4 Values whose Sum is 0
- UVA 10534 - Wavio Sequence
- CSU 1080: A simple maze
- UVA 10651 - Pebble Solitaire
- CSU 1248: 非变性聚丙烯酰胺凝胶电泳
- UVA 590 - Always on the run
- UVA 10306 - e-Coins
- UVA 10739 - String to Palindrome
- UVA 10304 - Optimal Binary Search Tree
- UVA 10271 - Chopsticks
- 第一周项目3 时间程序