当前位置: 代码迷 >> 综合 >> hdu 1026
  详细解决方案

hdu 1026

热度:74   发布时间:2023-12-14 22:47:00.0

用广搜即可做出来,程序思想,先把路径搜索出来,然后再在路径中去进行步骤的判断,结果的输出。


#include<iostream>
#include<queue>
#include<string>
using namespace std;
char arr[105][105];
int visit[105][105];
struct loc
{int x;int y;int parent;    //记录父亲节点,用于路径记录
};
loc loca[105];
int main()
{int n,m;while(cin>>n>>m){int i,j,res=0;for(i=0;i<n;i++)for(j=0;j<m;j++)cin>>arr[i][j];memset(visit,0,sizeof(visit));queue<loc> q;loc tmper;tmper.x=0;tmper.y=0;tmper.parent=-1;visit[0][0]=1;q.push(tmper);while(!q.empty()){loc l=q.front();q.pop();if(l.y!=m-1){if(!visit[l.x][l.y+1]&&arr[l.x][l.y+1]!='X'){loc tmp;tmp.x=l.x;tmp.y=l.y+1;tmp.parent=res;       visit[tmp.x][tmp.y]=1;q.push(tmp);}}if(l.x!=n-1){if(!visit[l.x+1][l.y]&&arr[l.x+1][l.y]!='X'){loc tmp;tmp.x=l.x+1;tmp.y=l.y;tmp.parent=res;visit[tmp.x][tmp.y]=1;q.push(tmp);}}if(l.y!=0){if(!visit[l.x][l.y-1]&&arr[l.x][l.y-1]!='X'){loc tmp;tmp.x=l.x;tmp.y=l.y-1;tmp.parent=res;visit[tmp.x][tmp.y]=1;q.push(tmp);}}if(l.x!=0){if(!visit[l.x-1][l.y]&&arr[l.x-1][l.y]!='X'){loc tmp;tmp.x=l.x-1;tmp.y=l.y;tmp.parent=res;visit[tmp.x][tmp.y]=1;q.push(tmp);}}				loca[res++]=l;}int record[100],count=0,sum=0;i=res-1;if(loca[i].x!=n-1&&loca[i].y!=m-1)cout<<"God please help our poor hero."<<endl;else{int xxx=-1;while(i!=-1){record[count++]=i;if(arr[loca[i].x][loca[i].y]>='1'&&arr[loca[i].x][loca[i].y]<='9'){sum+=(arr[loca[i].x][loca[i].y]-'0');xxx++;}elsesum+=1;i=loca[i].parent;}if(xxx!=-1)sum+=xxx;cout<<"It takes "<<sum <<" seconds to reach the target position, let me show you the way."<<endl;for(i=count-1,j=1;i>0;i--){if(arr[loca[record[i]].x][loca[record[i]].y]>='1'&&arr[loca[record[i]].x][loca[record[i]].y]<='9'){int ooo=arr[loca[record[i]].x][loca[record[i]].y]-'0';while(ooo--)cout<<j++<<"s:FIGHT AT "<<"("<<loca[record[i]].x<<","<<loca[record[i]].y<<")"<<endl;}cout<<j++<<"s:"<<"("<<loca[record[i]].x<<","<<loca[record[i]].y<<")->"<<"("<<loca[record[i-1]].x<<","<<loca[record[i-1]].y<<")"<<endl;}if(arr[loca[record[i]].x][loca[record[i]].y]>='1'&&arr[loca[record[i]].x][loca[record[i]].y]<='9'){int ooo=arr[loca[record[i]].x][loca[record[i]].y]-'0';while(ooo--)cout<<j++<<"s:FIGHT AT "<<"("<<loca[record[i]].x<<","<<loca[record[i]].y<<")"<<endl;}}cout<<"FINISH"<<endl;}return 0;
}