标签:des style blog http color io os ar java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1343 Accepted Submission(s): 642
题意和题解转自:http://blog.csdn.net/qq574857122/article/details/14649319
题意:
给定n*m的地图
#为墙 @为起点
下面K个坐标
问:遍历K个给定坐标,需要的最小步数
思路:
因为K 最大只有4
状压 当前是否走过某点
用二进制 的 i 位 0、1表示 第i个点是否走过
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<string> 10 11 #define N 105 12 #define M 15 13 #define mod 10000007 14 //#define p 10000007 15 #define mod2 100000000 16 #define ll long long 17 #define LL long long 18 #define maxi(a,b) (a)>(b)? (a) : (b) 19 #define mini(a,b) (a)<(b)? (a) : (b) 20 21 using namespace std; 22 23 int ans; 24 int n,m; 25 int k; 26 char s[N][N]; 27 int a[N][N]; 28 int mi[20][N][N]; 29 int dirx[]={1,0,-1,0}; 30 int diry[]={0,1,0,-1}; 31 32 typedef struct 33 { 34 int x; 35 int y; 36 int step; 37 int st; 38 }PP; 39 40 PP start; 41 42 void ini() 43 { 44 ans=-1; 45 int i,j,p; 46 int x,y; 47 for(i=0;i<n;i++){ 48 scanf("%s",s[i]); 49 } 50 memset(a,0,sizeof(a)); 51 scanf("%d",&k); 52 for(i=0;i<n;i++){ 53 for(j=0;j<m;j++){ 54 for(p=0;p<(1<<k);p++){ 55 mi[p][i][j]=1000000000; 56 } 57 if(s[i][j]==‘@‘){ 58 start.x=i; 59 start.y=j; 60 start.st=0; 61 start.step=0; 62 } 63 if(s[i][j]==‘#‘){ 64 a[i][j]=-1; 65 } 66 } 67 } 68 for(i=0;i<k;i++){ 69 scanf("%d%d",&x,&y); 70 a[x-1][y-1]=(1<<i); 71 if(x-1==start.x && y-1==start.y){ 72 start.st+=(1<<i); 73 mi[start.st][start.x][start.y]=0; 74 } 75 } 76 if(start.st==0){ 77 mi[0][start.x][start.y]=0; 78 } 79 80 // for(i=0;i<n;i++){ 81 // for(j=0;j<m;j++){ 82 // printf(" %d",a[i][j]); 83 // }printf("\n"); 84 // } 85 } 86 87 int ok(int i,int j) 88 { 89 if(i>=0 && i<n && j>=0 && j<m && a[i][j]!=-1){ 90 return 1; 91 } 92 return 0; 93 } 94 95 void solve() 96 { 97 int i; 98 PP now,nx; 99 queue<PP> q; 100 q.push(start); 101 while(q.size()>=1) 102 { 103 104 now=q.front(); 105 // printf(" i=%d j=%d st=%d step=%d\n",now.x,now.y,now.st,now.step); 106 q.pop(); 107 if(now.st== ((1<<k)-1) ){ 108 ans=now.step;return; 109 } 110 111 for(i=0;i<4;i++){ 112 nx=now; 113 nx.step++; 114 nx.x=now.x+dirx[i]; 115 nx.y=now.y+diry[i]; 116 if(ok(nx.x,nx.y)==0) continue; 117 if( (now.st & a[nx.x][nx.y])==0){ 118 nx.st=(now.st ^ a[nx.x][nx.y]); 119 // printf(" %d %d %d\n",now.st, a[nx.x][nx.y],nx.st); 120 //q.push(nx); 121 // mi[nx.st][nx.x][nx.y]=min(nx.step,mi[nx.st][nx.x][nx.y]); 122 } 123 //else{ 124 if( nx.step< mi[nx.st][nx.x][nx.y]){ 125 q.push(nx); 126 mi[nx.st][nx.x][nx.y]=nx.step; 127 } 128 // } 129 } 130 } 131 } 132 133 134 void out() 135 { 136 printf("%d\n",ans); 137 } 138 139 int main() 140 { 141 //freopen("data.in","r",stdin); 142 //freopen("data.out","w",stdout); 143 //scanf("%d",&T); 144 // for(int ccnt=1;ccnt<=T;ccnt++) 145 // while(T--) 146 while(scanf("%d%d",&n,&m)!=EOF) 147 { 148 if(n==0 && m==0 ) break; 149 //printf("Case %d: ",ccnt); 150 ini(); 151 solve(); 152 out(); 153 } 154 155 return 0; 156 }
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/njczy2010/p/4003614.html