G - 逃生
小A沦落到了一个迷宫之中,这个迷宫由n*m个格子构成,有些格子是不能通过的,现在他要从迷宫入口(1,1)的格子走到迷宫出口(n,m)的格子。
因为小A的方向感很弱,转多了会晕,所以他走到目的地的时候,最多能转z次,否则他就永远晕倒在原地了,到了出口也没法出去了。
请你告诉他最少能经过多少个格子走出迷宫。
小Y从(1,1)出发到达的第一个点都可以认为不用转向。
Input
有多组测试数据。
对于每组测试数据,第一行为3个整数n, m和z,表示n*m的迷宫,最多能转z次。
接下来是n*m的字符矩阵,仅由0和1表示,0表示可以通过,1表示不可以通过。
(2<=n,m<=100 ,1<=z<=50)
Output
对于每组测试数据,输出一行,包含一个整数,为经过的最少格子数。如果无法到达目的地,输出-1。
Sample Input
2 3 3 011 000 3 2 3 01 11 00 5 5 3 00000 00000 00101 01000 00000
Sample Output
4 -1 9
思路:
1.创建结构体,包含 y,x 转向 turn, 以及步数 sta。
广度优先遍历,如果这个 位置满足要求,则对着一个方向进行循环增加,直到无法再在一个方向探索为止。
代码(AC):
#include<iostream>
#include<queue>
using namespace std;
const int MAXN = 105;
struct node{
int y,x,turn,sta;
node(int y,int x,int turn,int sta):y(y),x(x),turn(turn),sta(sta){}
};
char map[MAXN][MAXN];
int N,M,Z;
const int dy[4] = {1,0,-1,0};
const int dx[4] = {0,1,0,-1};
bool if_ok(int y2,int x2){
return (0<= x2 && x2<M && 0<=y2 && y2 < N && map[y2][x2] == '0');
}
//void output(){
// printf("----------------\n");
// for(int i=0;i<N;i++){
// printf("%s",map[i]);
// printf("\n");
// }
//}
int bfs(int y,int x){
queue<node> que;
que.push(node(y,x,-1,1));
while(!que.empty()){
node temp = que.front(); que.pop();
for(int i=0;i<4;i++){
int y2 = temp.y + dy[i];
int x2 = temp.x + dx[i];
int turn2 = temp.turn+1;
int sta2 = temp.sta;
while(if_ok(y2,x2)){
sta2++;
map[y2][x2] = '1';
// output();
if(turn2 > Z)break;
if(y2 ==N-1 && x2 == M-1){
return sta2;
}
que.push(node(y2,x2,turn2,sta2));
y2 = y2 + dy[i];
x2 = x2 + dx[i];
}
}
}
return -1;
}
int main(){
while(scanf("%d%d%d",&N,&M,&Z)!=EOF){
for(int i=0;i<N;i++){
scanf("%s",map[i]);
}
int res = bfs(0,0);
printf("%d\n",res);
}
return 0;
}
错误代码,另外一种思路,但是无法达到目的:
#include <iostream>
#include<queue>
#include<stdio.h>
using namespace std;
int dy[4]={1,0,-1,0};
int dx[4]={0,1,0,-1};
const int MAXN = 105;
char map[MAXN][MAXN];
int N,M,Z;
int min_ste;
const int INF =0x3f3f3f3f;
struct node{
int y,x,z,sta,dir;
node():y(0),x(0),z(0),sta(0),dir(0){};
node(int y,int x,int z,int sta,int dir):y(y),x(x),z(z),sta(sta),dir(dir){};
};
int bfs(){
queue<node> que;
que.push(node(0,0,-1,1,0));
map[0][0] = '1';
// min_ste = INF;
while(!que.empty()){
node temp = que.front(); que.pop();
node nnode;
for(int i=0;i<4;i++){
int y2 = temp.y +dy[i];
int x2 = temp.x +dx[i];
if(0<=y2 && y2<N && 0<=x2 && x2<M && map[y2][x2]=='0'){
map[y2][x2] = '1';
int z2 = temp.z;
int sta2 = temp.sta+1;
if(temp.dir != i) z2++;
if(z2>Z) continue;
if(x2 ==M-1 && y2 == N-1){
return sta2;
}
nnode.y = y2;
nnode.x = x2;
nnode.z = z2;
nnode.sta = sta2;
nnode.dir = i;
que.push(nnode);
}
}
}
return -1;
}
void dfs(int x,int y,int z,int dir,int sta){
if(z>=Z) return;
if(x == M-1 && y == N-1) {
if(sta<min_ste)
min_ste = sta;
return;
}
for(int i=0;i<4;i++){
int y2 = y +dy[i];
int x2 = x +dx[i];
if(0<=y2 && y2<N && 0<=x2 && x2<M && map[y2][x2]=='0'){
map[y2][x2] = '1';
int z2 = z;
int sta2 = sta+1;
if(dir != i){
if(x != 0 || y != 0 ){
z2=z+1;
}
}
dfs(x2,y2,z2,i,sta2);
}
}
}
int main() {
while(scanf("%d %d %d",&N,&M,&Z)!=EOF){
for(int i=0;i<N;i++){
getchar();
for(int j=0;j<M;j++){
scanf("%c",&map[i][j]);
}
}
// min_ste = INF;
int res = bfs();
printf("%d\n",res);
// if(min_ste==INF) printf("-1\n");
// else printf("%d\n",min_ste);
}
return 0;
}