Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
有人帮忙看看是哪里错了吗? 我的思路是求出每个2个点之间的距离然后dfs#include<iostream> #include<queue> #define inf 999999999 using namespace std; struct node { int x, y; }dirty_point[15]; struct Node { int x, y, step; }; char map[25][25]; bool vis[25][25], dd[15]; int bb[15][15];// bfs int dir[4][2] = {-1, 0, 1, 0, 0 , -1, 0, 1}; int w, h, minMove, flag; queue<Node> qq; bool inmap(int x, int y) { return (x >= 0 && x < h && y >= 0 && y < w && map[x][y] != 'x'); } void dfs(int x, int sum, int count, int n) { int i; if(count == n) { if(sum < minMove) minMove = sum; return ; } if(sum > minMove) return ; for(i = 0; i < n; i++) { if(!dd[i] && bb[x][i] != 0 && x != i) { dd[i] = true; dfs(i, sum + bb[i][x], count + 1, n); dd[i] = false; } } } int bfs(int x, int y) { int i, j, k; Node tmp, now; while(!qq.empty()) { now = qq.front(); qq.pop(); for(k = 0; k < 4; k++) { i = now.x + dir[k][0]; j = now.y + dir[k][1]; if(!inmap(i, j) || vis[i][j]) continue; if(i == dirty_point[y].x && j == dirty_point[y].y) { bb[x][y] = now.step + 1; bb[y][x] = now.step + 1; return now.step + 1; } tmp.x = i; tmp.y = j; tmp.step = now.step + 1; qq.push(tmp); vis[i][j] = true; } } return -1; } int main() { int i, j; while(scanf("%d %d", &w, &h) == 2) { int n = 1; if(w == 0 && h == 0) break; memset(bb, 0, sizeof(bb)); memset(dd, false, sizeof(dd)); minMove = inf; for(i = 0; i < h; i++) { scanf("%s", &map[i]); for(j = 0; j < w; j++) { if(map[i][j] == 'o') { dirty_point[0].x = i; dirty_point[0].y = j; } if(map[i][j] == '*') { dirty_point[n].x = i; dirty_point[n++].y = j; } } } for(i = 0; i <= n-2; i++) for(j = i+1; j <= n-1; j++) { memset(vis, false, sizeof(vis)); while(!qq.empty()) qq.pop(); Node tmp; tmp.x = dirty_point[i].x; tmp.y = dirty_point[i].y; tmp.step = 0; qq.push(tmp); vis[tmp.x][tmp.y] = true; flag = bfs(i,j); if(flag == -1) { printf("-1\n"); break; } } if(flag == -1) continue; dd[0] = true; dfs(0, 0, 1, n); printf("%d\n", minMove); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator