您的位置:首页 > 其它

UVa 784 Maze Exploration(DFS)

2013-04-20 21:48 399 查看
这道题比较难办的是输入,有空格,要用gets();然后就是本小妞有马虎了,多写了一个else,结果半天没有音讯!还有就是,使得这个点不超范围,范围是要用&&来判断的,今天耍小聪明||了一下果断错了!

然后‘-’和‘_',减号和下划线今天还打混了!!!这些都要注意了

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int N = 100;
int T, sx, sy;
int a[] = { 0, 0, 1, -1 };
int b[] = { 1, -1, 0, 0 };
int cnt = 0, end = 0;
char g

;

void dfs( int x, int y ) {
g[x][y] = '#';
for ( int i = 0; i < 4; ++i ) {
int xx = x + a[i], yy = y + b[i];
if ( ( xx >= 0 && xx < cnt - 1 ) && ( y >= 0 && y < strlen(g[xx]) ) && ( g[xx][yy] == ' ' || g[xx][yy] == '_' ) ) dfs ( xx, yy );
}
}
int main()
{
scanf("%d", &T);
getchar();
while ( T-- ) {
memset( g, '\0', sizeof(g));
cnt = end = 0;
while ( gets( g[cnt] ) ) {
end = 1;
for ( int i = 0; i < strlen(g[cnt]); ++i ) {
if ( g[cnt][i] != '_' ) end = 0;
if ( g[cnt][i] == '*' ) {
sx = cnt, sy = i;
}
}
cnt++;
if ( end == 1 ) break;
}
dfs( sx, sy );
for ( int i = 0; i < cnt; ++i ) printf("%s\n", g[i]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: