HDU1728
for(int i = 0;i < 4;i++){
int nx = x + dx[i], ny = y + dy[i];
while(judge(nx,ny)){// 始终沿着一个方向走
if(!vis[nx][ny]){
...
}
nx += dx[i];
ny += dy[i];
}
}
一开始写的时候把上面的代码写错了,没有搜索全部的状态。
错误代码
for(int i = 0;i < 4;i++){
int nx = x + dx[i], ny = y + dy[i];
while(judge(nx,ny)){// 这样写遇到已经搜索过的就会停止直走,是错误的。
if(!vis[nx][ny]){
...
nx += dx[i];
ny += dy[i];
}
}
}
AC码
#include
#include
using namespace std;
struct Node {
int x, y;
char d;// 位于该节点的朝向
Node(int a = 0, int b = 0, char c = '/') {
x = a, y = b, d = c;
}
};
char maze[105][105];
int record[105][105];
int vis[105][105];
int dx[] = { 0,-1,0,1 };
int dy[] = { -1,0,1,0 };
int k, sx, sy, gx, gy, m, n;
void bfs() {// 以最少的转弯次数走到出口
memset((vis), -1, sizeof(vis));
memset((record), -1, sizeof(record));
int flag = -1;
vis[sx][sy] = 1;
queue<Node> que;
que.push(Node(sx, sy, '/'));
while (!que.empty()) {
Node u = que.front();
que.pop();
int x = u.x, y = u.y;
if (x == gx && y == gy) {
if (record[gx][gy] <= k) {
cout << "yes" << endl;
flag = 1;
}
break;
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
while ((0 <= nx && nx < m) && (0 <= ny && ny < n) && (maze[nx][ny] != '*')) {// 一直往一个方向走
if (vis[nx][ny] == -1) {// 没有访问过
char curD;
if (dx[i] == 0 && dy[i] == -1) curD = 'a';
if (dx[i] == -1 && dy[i] == 0) curD = 'b';
if (dx[i] == 0 && dy[i] == 1) curD = 'c';
if (dx[i] == 1 && dy[i] == 0) curD = 'd';
if (curD != u.d)
record[nx][ny] = record[x][y] + 1;
else
record[nx][ny] = record[x][y];
vis[nx][ny] = 1;
que.push(Node(nx, ny, curD));
}
nx += dx[i];
ny += dy[i];
}
}
}
if (flag == -1)cout << "no" << endl;
}
int main()
{
int t;
cin >> t;
while (t--) {
cin >> m >> n;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> maze[i][j];
cin >> k >> sy >> sx >> gy >> gx;
sx--, gx--, sy--, gy--;
bfs();
}
return 0;
}
DFS做法应该是比BFS更容易想到的做法。其思路主要是,搜索从起点到迷宫上任意可行点的路径,并记录从起点到每个可行点的最少转弯次数。
首先把DFS的整体框架写出来
void dfs(int x, int y, int d){// d表示朝向
if(x==gx&&y==gy&&turn[x][y]<=k){
flag = 1;
return;
}
for(int i = 0; i < 4; i ++){
int nx = x + dx[i], ny = y + dy[i];
if(judge(nx,ny)){// 该点可行
if ((i == 0 && d == 2) || (i == 1 && d == 3) || (i == 2 && d == 0) || (i == 3 && d == 1)) continue;// 这里防止DFS过程走回头路
//...这里做记录最小转弯次数的操作
dfs(nx, ny, i)
}
if(flag == 1) return;// 已经找到符合条件的路径
}
}
整体的框架就完成了,接下来是分析如何记录最小的转弯次数
if (turn[nx][ny] < turn[x][y]) continue;// 已经存在更小转弯次数
if (i != d) {
if (turn[nx][ny] < turn[x][y] + 1) continue;
turn[nx][ny] = turn[x][y] + 1;
}
else
turn[nx][ny] = turn[x][y];
AC码
#include
using namespace std;
int k, sx, sy, gx, gy, flag, m, n;
char maze[105][105];
int turn[105][105];
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
void dfs(int x, int y, int d) {
if (x == gx && y == gy && turn[x][y] <= k) {
flag = 1;
return;
}
if (turn[x][y] > k)return;// 剪枝
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if ((0 <= nx && nx < m) && (0 <= ny && ny < n) && (maze[nx][ny] != '*')) {
if ((i == 0 && d == 2) || (i == 1 && d == 3) || (i == 2 && d == 0) || (i == 3 && d == 1)) continue;
if (turn[nx][ny] < turn[x][y]) continue;
if (i != d) {
if (turn[nx][ny] < turn[x][y] + 1) continue;
turn[nx][ny] = turn[x][y] + 1;
}
else
turn[nx][ny] = turn[x][y];
dfs(nx, ny, i);
if (flag == 1) return;
}
}
return;
}
int main()
{
int t;
cin >> t;
while (t--) {
flag = 0;
memset(turn, 10000, sizeof(turn));
cin >> m >> n;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> maze[i][j];
cin >> k >> sy >> sx >> gy >> gx;
sx--, sy--, gx--, gy--;
turn[sx][sy] = -1;
dfs(sx, sy, -1);
if (flag) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}