2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3
no yes
题意:问能不能在转弯次数不大于K的情况下从起始点到目标点。
思路:1, 访问一个方向时候要全部访问能够访问的点。
2.判断能够到达之后要判断转弯次数是否不大于K
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
const int M = 105;
char map[M][M];
int m, n, ex, ey, sx, sy, k;
//bool vis[M][M];
struct node{
int x, y;
};
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int vis[M][M];
bool limit(node st){
int x = st.x, y = st.y;
return (x < m&&x >= 0&& y >= 0&& y < n&&map[x][y] != '*');
}
bool match(node st){
return (ex == st.x&&st.y == ey);
}
bool bfs(){
queue<node > q;
memset(vis, -1, sizeof(vis));
node st;
vis[sx][sy] = -1;
/*for(int i = 0; i < 4; ++ i){
st.x = sx + dx[i], st.y = sy + dy[i];
if(limit(st)){
vis[st.x][st.y] = 0;
if(match(st)) return 1;
q.push(st);
}
}*/
st.x = sx; st.y = sy;
q.push(st);
while(!q.empty()){
node temp = q.front(); q.pop();
for(int i = 0; i < 4; ++ i){
node cur = temp;
cur.x += dx[i]; cur.y += dy[i];
while(limit(cur)){
if(vis[cur.x][cur.y] == -1){
q.push(cur);
vis[cur.x][cur.y] = vis[temp.x][temp.y] + 1;
if(match(cur)) return 1;
}
cur.x += dx[i]; cur.y += dy[i];
}
}
}
return false;
}
int main(){
int t;
scanf("%d", &t);
while(t --){
scanf("%d%d", &m, &n);
for(int i = 0; i < m; ++ i){
scanf("%s", map[i]);
}
scanf("%d%d%d%d%d", &k, &ey, &ex, &sy, &sx);
sy--; ex --; sx--; ey--;
if(sy == ey && sx == ex){
printf("yes\n"); continue;
}
if(bfs())
printf("%s\n", vis[ex][ey] <= k?"yes":"no");
else printf("no\n");
}
return 0;
}原文:http://blog.csdn.net/shengweisong/article/details/46290407