标签:des style blog http color java os io strong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2354 Accepted Submission(s): 843
1 #include<cstring> 2 #include<cstdio> 3 #include<queue> 4 #include<vector> 5 using namespace std; 6 7 const int maxn=5003; 8 int cc,rr,cost; 9 char str[maxn][maxn]; 10 struct node { 11 int x,y; 12 int val; 13 }; 14 vector <node> pot; 15 16 int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; 17 node st,en; 18 queue<node>seek; 19 node tem,qq; 20 vector<node>::iterator it; 21 int main(){ 22 23 //freopen("test.in","r",stdin); 24 while(scanf("%d%d%d",&rr,&cc,&cost)!=EOF){ 25 26 pot.clear(); 27 vector< vector<node> > map(rr+10); 28 for(int i=0;i<rr+10;i++) 29 map[i].resize(cc+10); 30 for(int i=0;i<rr;i++){ 31 scanf("%s",str[i]); 32 for(int j=0;j<cc;j++){ 33 map[i][j]=(node){i,j,0}; 34 if(str[i][j]==‘P‘){ 35 pot.push_back((node){i,j,0}); 36 } 37 if(str[i][j]==‘Y‘) 38 st=(node){i,j,0}; 39 if(str[i][j]==‘C‘){ 40 en=(node){i,j,-1}; 41 map[i][j]=(node){i,j,-1}; 42 } 43 } 44 } 45 //bfs(); 46 seek.push(st); 47 while(!seek.empty()){ 48 tem=seek.front(); 49 seek.pop(); 50 for(int i=0;i<4;i++){ 51 qq=(node){tem.x+dir[i][0],tem.y+dir[i][1],0}; 52 if(qq.x>=0&&qq.x<rr&&qq.y>=0&&qq.y<cc&&str[qq.x][qq.y]!=‘#‘){ 53 if(str[qq.x][qq.y]!=‘C‘){ 54 if(str[qq.x][qq.y]==‘P‘){ 55 str[qq.x][qq.y]=‘#‘; 56 if(map[qq.x][qq.y].val==0||map[qq.x][qq.y].val>map[tem.x][tem.y].val) 57 map[qq.x][qq.y].val=map[tem.x][tem.y].val; 58 if(pot.size()!=0){ 59 for(it=pot.begin();it<pot.end();it++) 60 if((*it).x==qq.x&&(*it).y==qq.y) break; 61 pot.erase(it); 62 } 63 if(pot.size()!=0){ 64 for(it=pot.begin();it<pot.end();it++){ 65 map[(*it).x][(*it).y].val=map[qq.x][qq.y].val; 66 seek.push((map[(*it).x][(*it).y])); 67 str[(*it).x][(*it).y]=‘#‘; 68 } 69 }else seek.push(map[qq.x][qq.y]); 70 71 } 72 else if(map[qq.x][qq.y].val==0||map[qq.x][qq.y].val>tem.val+cost){ 73 map[qq.x][qq.y].val=tem.val+cost; 74 seek.push(map[qq.x][qq.y]); 75 } 76 } 77 else 78 if(map[qq.x][qq.y].val==-1||map[qq.x][qq.y].val>tem.val) 79 { 80 map[qq.x][qq.y].val=tem.val; 81 } 82 } 83 } 84 } 85 if(map[en.x][en.y].val==-1) 86 printf("Damn teoy!\n"); 87 else 88 printf("%d\n",map[en.x][en.y].val); 89 } 90 return 0; 91 }
hdu----(4308)Saving Princess claire_(搜索)
标签:des style blog http color java os io strong
原文地址:http://www.cnblogs.com/gongxijun/p/3933048.html