这是一份通过了测试样例,提交会TLE,但是全程由我自己敲的代码,TLE的原因是进行了大量的坐标运算和map查找,没有把点弄成hash,根据ID访问点,不想改了,放在博客上,主要具有纪念意义。
1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 using namespace std; 4 5 struct Node 6 { 7 int x,y; 8 inline bool operator ==(const Node &rhs) const 9 { 10 return x==rhs.x&&y==rhs.y; 11 } 12 bool operator <(const Node &rhs) const 13 { 14 if(x != rhs.x) 15 return x < rhs.x; 16 else 17 return y < rhs.y; 18 } 19 }; 20 21 int w,h,n; 22 char m[17][17]; 23 map<Node,int> sm; 24 Node a,b,c,A,B,C; 25 vector<vector<Node>> v; 26 int d[253][253][253]; 27 const int dx[] = {0,0,1,-1}; 28 const int dy[] = {1,-1,0,0}; 29 30 int limit(int x,int y) 31 { 32 return x>=0&&x<w&&y>=0&&y<h; 33 } 34 35 void input() 36 { 37 sm.clear(); 38 v.clear(); 39 memset(m,0,sizeof(m)); 40 memset(d,-1,sizeof(d)); 41 _for(i,0,w) 42 { 43 _for(j,0,h) 44 { 45 char tmp;scanf("%c",&tmp); 46 if(tmp==' ') m[i][j] = ' '; 47 else if(tmp == '#')m[i][j] = '#'; 48 else if(tmp == 'a') {a.x = i,a.y = j;m[i][j] = ' ';} 49 else if(tmp == 'b') {b.x = i,b.y = j;m[i][j] = ' ';} 50 else if(tmp == 'c') {c.x = i,c.y = j;m[i][j] = ' ';} 51 else if(tmp == 'A') {A.x = i,A.y = j;m[i][j] = ' ';} 52 else if(tmp == 'B') {B.x = i,B.y = j;m[i][j] = ' ';} 53 else if(tmp == 'C') {C.x = i,C.y = j;m[i][j] = ' ';} 54 } 55 getchar(); 56 } 57 58 _for(i,0,w) 59 { 60 _for(j,0,h) 61 { 62 if(m[i][j]!='#') 63 { 64 vector<Node> tmpv; 65 Node tmpn;tmpn.x = i;tmpn.y = j; 66 tmpv.push_back(tmpn); 67 _for(k,0,4) 68 { 69 int nnx = dx[k]+i; 70 int nny = dy[k]+j; 71 if(limit(nnx,nny)) 72 { 73 Node tmpn2; 74 if(m[nnx][nny]!='#') 75 { 76 tmpn2.x = nnx,tmpn2.y = nny; 77 tmpv.push_back(tmpn2); 78 } 79 } 80 } 81 v.push_back(tmpv); 82 sm[tmpn] = v.size()-1; 83 } 84 } 85 } 86 } 87 88 struct state 89 { 90 int a,b,c; 91 }; 92 93 int solve() 94 { 95 queue<state> q; 96 d[sm[a]][sm[b]][sm[c]] = 0; 97 state ori;ori.a = sm[a],ori.b = sm[b],ori.c = sm[c]; 98 q.push(ori); 99 while(!q.empty()) 100 { 101 state u = q.front();q.pop(); 102 if(u.a==sm[A]&&u.b==sm[B]&&u.c==sm[C]) 103 { 104 return d[sm[A]][sm[B]][sm[C]]; 105 } 106 _for(i,0,v[u.a].size()) 107 { 108 Node newa = v[u.a][i]; 109 _for(j,0,v[u.b].size()) 110 { 111 Node newb = v[u.b][j]; 112 if(v[u.a][i]==v[u.b][j]||v[u.a][0]==v[u.b][j]&&v[u.b][0]==v[u.a][i]) 113 continue; 114 _for(k,0,v[u.c].size()) 115 { 116 Node newc = v[u.c][k]; 117 if(v[u.a][i]==v[u.c][k]||v[u.b][j]==v[u.c][k] 118 ||v[u.a][0]==v[u.c][k]&&v[u.c][0]==v[u.a][i] 119 ||v[u.b][0]==v[u.c][k]&&v[u.c][0]==v[u.b][j]) 120 continue; 121 state ttmp;ttmp.a = sm[newa];ttmp.b = sm[newb];ttmp.c = sm[newc]; 122 if(d[ttmp.a][ttmp.b][ttmp.c]<0) 123 { 124 d[ttmp.a][ttmp.b][ttmp.c] = d[u.a][u.b][u.c]+1; 125 q.push(ttmp); 126 } 127 } 128 } 129 } 130 } 131 } 132 133 int main() 134 { 135 while(cin >> h >> w >> n && h!=0) 136 { 137 getchar(); 138 input(); 139 if(n<=2) 140 { 141 vector<Node> tmpv; 142 Node tmpn;tmpn.x = -1;tmpn.y = -1; 143 tmpv.push_back(tmpn); 144 v.push_back(tmpv); 145 sm[tmpn] = v.size()-1; 146 c.x = -1,c.y = -1; 147 C.x = -1,C.y = -1; 148 } 149 if(n<=1) 150 { 151 vector<Node> tmpv; 152 Node tmpn;tmpn.x = -2;tmpn.y = -2; 153 tmpv.push_back(tmpn); 154 v.push_back(tmpv); 155 sm[tmpn] = v.size()-1; 156 b.x = -2,b.y = -2; 157 B.x = -2,B.y = -2; 158 } 159 int rnt = solve(); 160 printf("%d ",rnt); 161 } 162 return 0; 163 }