单开vis数组提交记录:这里
Code:
#include<bits/stdc++.h>
using namespace std;
struct Pair{
int x;
int y;
};
const int R=150;
const int dx[]={0,0,-1,0,1};
const int dy[]={0,1,0,-1,0};
int r,c;
bool flag = false;
char a[R][R];
Pair b[R*R];
int now=1;
bool book[R][R];
void push_back(Pair x){
b[now]=x;
now++;
}
void pop_back(){
now--;
}
void dfs(int x,int y){
if(x==r&&y==c){
flag = true;
return;
}
for(int i=1;i<=4;i++){
int fx=x+dx[i];
int fy=y+dy[i];
if(fx<1||fx>r||fy<1||fy>c) continue;
if(book[fx][fy]==1) continue;
if(a[fx][fy]=='*') continue;
book[fx][fy]=true;
push_back(Pair{fx,fy});
dfs(fx,fy);
if(flag) return;
pop_back();
a[fx][fy]='.';
}
}
int main(void){
scanf("%d%d",&r,&c);
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
cin>>a[i][j];
}
}
push_back(Pair{1,1});
a[1][1]='#';
dfs(1,1);
for(int i=1;i<now;i++){
cout<<b[i].x<<" "<<b[i].y<<endl;
}
return 0;
}
修改地图提交记录:这里
Code:
#include<bits/stdc++.h>
using namespace std;
struct Pair{
int x;
int y;
};
const int R=150;
const int dx[]={0,0,-1,0,1};
const int dy[]={0,1,0,-1,0};
int r,c;
bool flag = false;
char a[R][R];
Pair b[R*R];
int now=1;
void push_back(Pair x){
b[now]=x;
now++;
}
void pop_back(){
now--;
}
void dfs(int x,int y){
if(x==r&&y==c){
flag = true;
return;
}
for(int i=1;i<=4;i++){
int fx=x+dx[i];
int fy=y+dy[i];
if(fx<1||fx>r||fy<1||fy>c) continue;
if(a[fx][fy]=='#') continue;
if(a[fx][fy]=='*') continue;
a[fx][fy]='#';
push_back(Pair{fx,fy});
dfs(fx,fy);
if(flag) return;
pop_back();
a[fx][fy]='.';
}
}
int main(void){
scanf("%d%d",&r,&c);
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
cin>>a[i][j];
}
}
push_back(Pair{1,1});
a[1][1]='#';
dfs(1,1);
if(flag==0) cout<<-1;
else{
for(int i=1;i<now;i++){
cout<<b[i].x<<" "<<b[i].y<<endl;
}
}
return 0;
}
求大佬解答QWQ