#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<queue> #include<vector> #include<sstream> #include<cmath> #include<queue> #include<cctype> using namespace std; const int maxn = 16+2, maxc = 18*18; int w, h, n; char buf[maxn][maxn]; int deg[maxc], G[maxc][5]; const int dx[] = {0,1,0,-1,0}; const int dy[] = {0,0,1,0,-1}; int s[3],t[3]; int d[maxc][maxc][maxc]; int ID(int a, int b, int c) { return (a<<16)|(b<<8)|c; } bool conflict(int aa,int bb,int a,int b){ if((aa == bb)||(aa == b && bb == a)) return true; return false; } int solve(){ memset(d, -1, sizeof(d)); queue<int>q; d[s[0]][s[1]][s[2]] = 0; q.push(ID(s[0],s[1],s[2])); while(!q.empty()){ int u = q.front(); q.pop(); int a = (u>>16)&0xff, b = (u>>8)&0xff, c = u&0xff; for(int i = 0; i < deg[a]; i++){ int a2 = G[a][i]; for(int j = 0; j < deg[b]; j++){ int b2 = G[b][j]; if(conflict(a2,b2,a,b)) continue; for(int k = 0; k < deg[c]; k++){ int c2 = G[c][k]; if(d[a2][b2][c2]!=-1) continue; if(conflict(a2,c2,a,c)) continue; if(conflict(b2,c2,b,c)) continue; d[a2][b2][c2] = d[a][b][c] +1; if(a2 == t[0] && b2 == t[1] && c2 == t[2]) return d[a2][b2][c2]; q.push(ID(a2,b2,c2)); } } } } return -1; } int main(){ while(scanf("%d%d%d\n",&w,&h,&n) == 3 && w){ for(int i = 0; i < h; i++) fgets(buf[i],maxn,stdin); memset(s,-1,sizeof(s)); memset(t,-1,sizeof(t)); int cur = 0; int x[maxc], y[maxc], id[maxn][maxn]; memset(id,-1,sizeof(id)); for(int i =0; i < h; i++) for(int j = 0; j < w; j++){ if(buf[i][j]!='#'){ id[i][j] = cur; x[cur] = i; y[cur] = j; if(islower(buf[i][j])) s[buf[i][j]-'a'] = cur; else if(isupper(buf[i][j])) t[buf[i][j]-'A'] = cur; cur++; } } for(int k = 0; k < cur; k++){ deg[k] = 0; for(int i = 0; i < 5; i++){ int xx = x[k] + dx[i]; int yy = y[k] + dy[i]; if(buf[xx][yy]!='#'){ G[k][deg[k]++] = id[xx][yy]; } } } if(n <= 2){deg[cur] = 1;s[2] = t[2] = cur;G[cur][0] = cur;cur++;} if(n <= 1){deg[cur] = 1;s[1] = t[1] = cur;G[cur][0] = cur;} printf("%d\n",solve()); } return 0; }
不服?还不够暴力?? 求指点
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<queue> #include<vector> #include<sstream> #include<cmath> #include<queue> #include<cctype> using namespace std; const int maxn = 16+2, maxc = 16*16*16*16*16*16; int w, h, n, GOAL; char buf[maxn][maxn]; bool visit[maxc]; const int dx[] = {0,1,0,-1,0}; const int dy[] = {0,0,1,0,-1}; struct node{ int x,y; }; struct Node{ node pos[3]; int temp; bool operator < (const Node &rhy) const{ return temp > rhy.temp; } }first,goal; bool inside(Node t){ for(int i = 0;i < n; i++){ int x = t.pos[i].x, y = t.pos[i].y; if(!(x>=0&&y>=0&&x<h&&y<w&&buf[x][y] != '#')) return false; } return true; } int f(Node t){ if(n == 3) return t.pos[0].x*16*16*16*16*16+t.pos[0].y*16*16*16*16+ t.pos[1].x*16*16*16+t.pos[1].y*16*16+ t.pos[2].x*16+t.pos[2].y; else if(n == 2){ return t.pos[0].x*16*16*16+t.pos[0].y*16*16+ t.pos[1].x*16+t.pos[1].y; } else { return t.pos[0].x*16+t.pos[0].y; } } void read_input(){ first.temp = 0; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ scanf("%c",&buf[i][j]); if(islower(buf[i][j])) first.pos[buf[i][j] - 'a'] = {i,j}; else if(isupper(buf[i][j])) goal.pos[buf[i][j] - 'A'] = {i,j}; } getchar(); } GOAL = f(goal); } bool g(Node t,Node s){ for(int i = 0;i < n;i++) for(int j = i+1;j < n ;j++){ if(memcmp(&t.pos[i],&t.pos[j],sizeof(node)) == 0) return false; } for(int i = 0;i < n;i++) for(int j = i+1;j < n ;j++){ if(memcmp(&t.pos[i],&s.pos[j],sizeof(node)) == 0 && memcmp(&t.pos[j],&s.pos[i],sizeof(node)) == 0) return false; } return true; } void solve(){ memset(visit,false,sizeof(visit)); priority_queue<Node>pq; pq.push(first); visit[f(first)] = true; while(!pq.empty()){ Node s = pq.top(); pq.pop(); for(int i = 0; i < 5; i++) for(int j = 0; j < 5; j++) for(int k = 0; k < 5; k++){ Node t; memcpy(&t,&s,sizeof(s)); t.pos[0].x += dx[i]; t.pos[0].y += dy[i]; t.pos[1].x += dx[j]; t.pos[1].y += dy[j]; t.pos[2].x += dx[k]; t.pos[2].y += dy[k]; if(inside(t)&&visit[f(t)] == false&&g(t,s)){ t.temp++; if(f(t) == GOAL){ printf("%d\n",t.temp); return; } pq.push(t); visit[f(t)] = true; } } } } int main(){ while(scanf("%d%d%d\n",&w,&h,&n) == 3 && w && h && n){ read_input(); solve(); } return 0; }