本文共 1033 字,大约阅读时间需要 3 分钟。
题目链接:
分析:直接深搜从起点到终点,如何取最短路线,其实只要优先向下或向右走即可。
#include #include #include #include #include #include #include #include #include #include #include #define LL long long#define mod 1000000007#define inf 0x3f3f3f3f#define N 100010using namespace std;int s[10][10];stack s1,s2;int vis[10][10];int judge(int a,int b){ return a>=0&&a<5&&b>=0&&b<5&&s[a][b]==0&&!vis[a][b];}int dfs(int x,int y){ if(x==4&&y==4) { s1.push(x);s2.push(y);return 1; } vis[x][y]=1; if(judge(x,y+1)&&dfs(x,y+1)||judge(x+1,y)&&dfs(x+1,y)|| judge(x-1,y)&&dfs(x-1,y)||judge(x,y-1)&&dfs(x,y-1)) { s1.push(x);s2.push(y);return 1; } else { return 0; } return 0;}void print(){ while(!s1.empty()) { printf("(%d, %d)\n",s1.top(),s2.top()); s1.pop();s2.pop(); }}int main(){ memset(vis,0,sizeof(vis)); for(int i=0;i<5;i++) for(int j=0;j<5;j++)scanf("%d",&s[i][j]); dfs(0,0);print();}
转载于:https://www.cnblogs.com/lienus/p/4167691.html