上一次打是在寒假,现在代码风格都有了很大的改变。
当时还是跟着伟大的红太阳 DQS(%%)学的呢。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int n;
char maps[233][233];
bool v[233][233];
int dx[] = {0,1,0,-1,0};
int dy[] = {0,0,1,0,-1};
int sx, sy, ex, ey;
struct loc{
int x, y;
};
queue <loc> q;
void bfs(int sx, int sy)
{
q.push((loc){sx,sy});
v[sx][sy] = 1;
while(q.size())
{
loc nw = q.front();
q.pop();
if(nw.x == ex && nw.y == ey){
cout << "YES" << endl;
return;
}
for(int i = 1; i <= 4; i ++)
{
int x = nw.x + dx[i];
int y = nw.y + dy[i];
if(x > 0 && x <= n && y > 0 && y <= n && maps[x][y] != '#' && !v[x][y])
{
q.push((loc){x,y});
v[x][y] = 1;
}
}
}
cout << "NO" << endl;
}
int main()
{
int t;
cin >> t;
cin >> n;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++){
cin >> maps[i][j];
if(maps[i][j] == 's')
sx = i,sy = j;
else if(maps[i][j] == 'e')
ex = i; ey = j;
}
bfs(sx,sy);
return 0;
}
以上:2016-06-11 21:33:37
2016-9-28 17:04:55
重打:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 20;
const int dx[] = {0,0,-1,0,1};
const int dy[] = {0,-1,0,1,0};
int n, sx, sy;
char maps[MAXN][MAXN];
struct Point{
int x, y;
};
queue <Point> q;
void bfs(int sx, int sy)
{
q.push((Point){sx,sy});
maps[sx][sy] = '#';
while(q.size())
{
Point now = q.front();
q.pop();
for(int i = 1; i <= 4; i ++)
{
int nx = now.x + dx[i];
int ny = now.y + dy[i];
if(maps[nx][ny] == 'e')
{
puts("YES");
return;
}
if(nx >= 0 && nx <= n && ny >= 0 && ny <= n && maps[nx][ny] == '.')
{
maps[nx][ny] = '#';
q.push((Point){nx, ny});
}
}
}
puts("NO");
}
void init()
{
while(q.size())
q.pop();
}
int main()
{
int T;
cin >> T;
while(T--)
{
init();
cin >> n;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
{
char c = getchar();
while(c != 's' && c != 'e' && c != '.' && c != '#')
c = getchar();
maps[i][j] = c;
if(c == 's')
sx = i, sy = j;
}
bfs(sx,sy);
}
return 0;
}