标签:方向 put turn inpu 接下来 间隔 input c++ 准备
#include <bits/stdc++.h> using namespace std; int move1[4][2]={-1,0,1,0,0,1,0,-1}; char map1[600][600]; int vis[600][600]; int n,m; struct node { int x; int y; int step; string c; }; int check(int x,int y) { if(x>=0&&y>=0&&x<n&&y<m&&map1[x][y]==‘0‘&&!vis[x][y]) return 1; return 0; } void bfs() { queue<node>Q; node a,next; a.x=0; a.y=0; a.step=0; Q.push(a); while(!Q.empty()) { //puts("1"); a=Q.front(); if(a.x==n-1&&a.y==m-1) { cout<<a.step<<endl; cout<<a.c<<endl; } Q.pop(); next.x=a.x+1; next.y=a.y; if(check(next.x,next.y)) { vis[next.x][next.y]=1; next.step=a.step+1; next.c=a.c+‘D‘; Q.push(next); } next.x=a.x; next.y=a.y-1; if(check(next.x,next.y)) { vis[next.x][next.y]=1; next.step=a.step+1; next.c=a.c+‘L‘; Q.push(next); } // puts("3"); next.x=a.x; next.y=a.y+1; // cout<<" "<<vis[0][1]<<endl; // cout<<" "<<map1[0][1]<<endl; // cout<<next.x<<" "<<next.y<<endl; if(check(next.x,next.y)) { // puts("2"); vis[next.x][next.y]=1; next.step=a.step+1; next.c=a.c+‘R‘; Q.push(next); } next.x=a.x-1; next.y=a.y; if(check(next.x,next.y)) { vis[next.x][next.y]=1; next.step=a.step+1; next.c=a.c+‘U‘; Q.push(next); } } } int main() { cin>>n>>m; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) cin>>map1[i]; vis[0][0]=1; bfs(); }
标签:方向 put turn inpu 接下来 间隔 input c++ 准备
原文地址:http://www.cnblogs.com/a249189046/p/6639389.html