http://acm.hdu.edu.cn/showproblem.php?pid=1175
题意:中文题不解释。
思路:核心就和hdu1728一样,思路请看我的分析。有了上一题的基础,这题就是割草小游戏了,1A~
好久木有1A了,感动
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
typedef long long LL;
const int N = 1005;
const int INF = 0x3f3f3f3f;
int G[N][N];
bool vis[N][N];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, m, sx, sy, ex, ey;
struct node
{
int x, y, step;
};
bool check(int x, int y)
{
if(x>=1 && x<=n && y>=1 && y<=m && G[x][y]==0) return true;
else return false;
}
void bfs(int x, int y)
{
if(x==ex && y==ey)//这里注意特殊情况
{
printf("YES\n");
return;
}
memset(vis, false, sizeof(vis));
queue<node>que;
node s;
s.x = x;
s.y = y;
s.step = -1;
vis[x][y] = true;
que.push(s);
while(!que.empty())
{
node tmp = que.front();
que.pop();
for(int i = 0; i < 4; i++)
{
node tmp2;
tmp2 = tmp;
tmp2.x += dir[i][0];
tmp2.y += dir[i][1];
while(check(tmp2.x, tmp2.y))
{
if(!vis[tmp2.x][tmp2.y])
{
vis[tmp2.x][tmp2.y] = true;
tmp2.step = tmp.step+1;
if(tmp2.x == ex && tmp2.y == ey && tmp2.step<=2)
{
printf("YES\n");
return;
}
que.push(tmp2);
}
tmp2.x += dir[i][0];
tmp2.y += dir[i][1];
}
}
}
printf("NO\n");
return;
}
int main()
{
// freopen("in.txt", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
if(n==0 && m==0) break;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
cin >> G[i][j];
}
int q;
scanf("%d", &q);
while(q--)
{
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
if(G[sx][sy]!=G[ex][ey] || G[sx][sy]==0 || G[ex][ey]==0) printf("NO\n");
else
{
int tmp = G[sx][sy];
G[sx][sy] = G[ex][ey] = 0;
bfs(sx, sy);
G[sx][sy] = G[ex][ey] = tmp;
}
}
}
return 0;
}
本文提供了一道 HDU 1175 的算法题解,采用广度优先搜索(BFS)策略解决迷宫路径问题。通过判断起点与终点的可达性和步数限制来确定解决方案的存在性。
&spm=1001.2101.3001.5002&articleId=52904787&d=1&t=3&u=135ba5609ec54b97a2d773ea06f0e980)
1728

被折叠的 条评论
为什么被折叠?



