标签:des style blog http java color
线上题目:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4927 Accepted Submission(s): 1377
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #include <vector> 7 #include <set> 8 #define INF (1<<30) 9 #define MAX 10 10 using namespace std; 11 12 typedef struct state{ 13 int by,bx; 14 int ry,rx; 15 int step; 16 17 bool operator < (const state& o)const{ 18 return step>o.step; 19 } 20 21 bool operator == (const state& o)const{ 22 return (by==o.by && bx==o.bx && ry==o.ry && rx==o.rx); 23 } 24 25 }state; 26 27 int cy[]={0,1,0,-1}; 28 int cx[]={-1,0,1,0}; 29 int minn; 30 int n,m; 31 int mp[MAX][MAX]; 32 int ey,ex; 33 vector<state> vv; 34 priority_queue<state> q; 35 int isok(state v){ 36 if(0<=v.ry && v.ry<n && 0<=v.rx && v.rx<m && mp[v.ry][v.rx]!=1){ 37 if(v.ry ==v.by && v.rx == v.bx) return -1; 38 return 1; 39 } 40 return 0; 41 } 42 43 bool isexist(state v){ 44 for(unsigned int i=0;i<vv.size();i++) if(vv[i] == v) return 1; 45 vv.push_back(v); 46 return 0; 47 } 48 49 bool canPush(state v,int dir){ 50 v.by+=cy[dir]; v.bx+=cx[dir]; 51 if(0<=v.by && v.by<n && 0<=v.bx && v.bx<m && mp[v.by][v.bx]==0) return 1; 52 return 0; 53 } 54 55 int bfs(){ 56 state u,v; 57 while(!q.empty()) q.pop(); 58 vv.clear(); 59 for(int i=0;i<n;i++) for(int j=0;j<m;j++) 60 if(mp[i][j]== 2){ 61 u.by = i; u.bx = j; mp[i][j] = 0; 62 }else if(mp[i][j] == 3){ 63 ey = i; ex = j; mp[i][j] = 0; 64 }else if(mp[i][j] == 4){ 65 u.ry = i; u.rx= j; mp[i][j] = 0; 66 } 67 u.step = 0; 68 q.push(u); 69 int co = 0; 70 while(!q.empty()){ 71 u = q.top(); 72 q.pop(); 73 co++; 74 for(int i=0;i<4;i++){ 75 v = u; 76 v.ry+=cy[i]; v.rx+=cx[i]; 77 int f = isok(v); 78 if(f == 0) continue; 79 else if(f == 1 && isexist(v)) continue; 80 else if(f == -1){ 81 if(!canPush(v,i)) continue; 82 v.by+=cy[i]; v.bx+=cx[i]; 83 v.step++; 84 if(v.by == ey && v.bx == ex){ return v.step ;} 85 if(isexist(v)) continue; 86 } 87 q.push(v); 88 } 89 } 90 return -1; 91 } 92 93 int main() 94 { 95 int t; 96 //freopen("ans.txt","r",stdin); 97 scanf("%d",&t); 98 while(t--){ 99 scanf("%d %d",&n,&m); 100 for(int i=0;i<n;i++) for(int j=0;j<m;j++) scanf("%d",&mp[i][j]); 101 int ans = bfs(); 102 printf("%d\n",ans); 103 } 104 return 0; 105 }
HDU - 1254 - 推箱子,布布扣,bubuko.com
标签:des style blog http java color
原文地址:http://www.cnblogs.com/sineatos/p/3847345.html