HDU 1241 HDU 1495 HDU2612
蒻苣:弱弱很弱,路过的巨巨还不吝赐教!^.^...QAQ
三题都分类为搜索。。水搜索。。。
不过HDU 1495 貌似模拟也行的。。。
HDU 1241 Oil Deposits
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241
题意:给出一个n*m的地图,求油田的数量。 其中 上下等八个方向只要相连就算作一个油田。
题解:BFS或者DFS。 数据不大,都可以做。
注意:题面输入的地方有个坑,就是输入n和m后面有个空格。。用scanf的小心。。。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 char s[200+5][200+5]; 8 int vis[200+5][200+5]; 9 int dir[8][2] = {1,1,-1,-1,1,-1,-1,1,1,0,-1,0,0,1,0,-1}; 10 struct st{ 11 int x,y; 12 }; 13 queue<st>q; 14 void bfs(int x,int y,int n,int m) 15 { 16 st f; 17 f.x = x; 18 f.y = y; 19 vis[x][y] = 1; 20 q.push(f); 21 while( !q.empty() ) 22 { 23 f = q.front(); 24 st g; 25 q.pop(); 26 for(int i = 0;i < 8; i++) 27 { 28 g.x = f.x + dir[i][0]; 29 g.y = f.y + dir[i][1]; 30 if(!vis[g.x][g.y]&&s[g.x][g.y]=='@') 31 { 32 q.push(g); 33 vis[g.x][g.y] = 1; 34 } 35 } 36 } 37 } 38 int main() 39 { 40 int n,m; 41 while(cin>>n>>m) 42 { 43 if(!m) break; 44 memset(vis,0,sizeof(vis)); 45 memset(s,0,sizeof(s)); 46 int x1,x2,y1,y2; 47 for(int i = 1; i <= n; i++ ) cin>>s[i]+1; 48 int sum = 0; 49 for(int i = 1;i <= n; i++) 50 for(int j = 1; j <= m;j++ ) 51 if(s[i][j] == '@' && !vis[i][j]) 52 {bfs(i,j,n,m);sum++;} 53 printf("%d\n",sum); 54 } 55 return 0; 56 }
HDU 1495 非常可乐
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495
题意:中文题。。。
题解:模拟或者搜索都行吧。。模拟 0ms过的 搜索 15ms(其实也差不多是0)
模拟代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int sum; 7 void dfs(int m,int n,int k) 8 { 9 if(n<k) 10 swap(n,k); 11 int a,b,c; 12 sum = 1; 13 b = n; 14 c = 0; 15 a = k; 16 while(a!=m) 17 { 18 if(c==0) 19 { 20 if(b>=k) 21 { 22 c=k; 23 b-=c; 24 } 25 else 26 { 27 c=b; 28 b=0; 29 } 30 sum++; 31 } 32 else 33 { 34 if(c==k) 35 { 36 a+=c; 37 c=0; 38 sum++; 39 } 40 else if( b==0) 41 { 42 b=n; 43 a-=b; 44 sum++; 45 } 46 else if(b>0) 47 { 48 if(b>=k-c) 49 { 50 int t = k-c; 51 c=k; 52 b-=t; 53 } 54 else 55 { 56 b=0; 57 c+=b; 58 } 59 sum++; 60 } 61 } 62 if(a==b+c) 63 break; 64 } 65 if(a == m) 66 sum = 0; 67 } 68 69 int main() 70 { 71 int m,n,k; 72 while(cin>>m>>n>>k) 73 { 74 sum = 0; 75 if( m == 0 && n == 0 && k == 0) 76 break; 77 if(n+k<m) 78 cout<<"NO"<<endl; 79 else if(n==k) 80 cout<<"1"<<endl; 81 else{ 82 dfs(m,n,k); 83 if(sum) 84 cout<<sum<<endl; 85 else 86 cout<<"NO"<<endl; 87 } 88 } 89 }
搜索代码:(略丑)
1 #include<cstring> 2 #include<cstdlib> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<iostream> 7 using namespace std; 8 int used[105][105];//判断两个杯子装可乐的情况是否出现过 9 struct node 10 { 11 int m,n,s,t; 12 }; 13 int m,n,s; 14 int bfs() 15 { 16 queue<node> q; 17 node now,next; 18 now.m=now.n=now.t=0; 19 now.s=s; 20 q.push(now); 21 memset(used,0,sizeof(used)); 22 used[0][0]=1; 23 while(!q.empty()) 24 { 25 now=q.front(); 26 q.pop(); 27 if(now.n==now.s&&now.s==s/2) 28 return now.t; 29 if(now.m+now.s>m) //s->m 30 { 31 next.m=m,next.n=now.n,next.s=now.m+now.s-m,next.t=now.t+1; 32 if(used[next.m][next.n]==0) 33 { 34 used[next.m][next.n]=1; 35 q.push(next); 36 } 37 } 38 else 39 { 40 next.m=now.m+now.s,next.n=now.n,next.s=0,next.t=now.t; 41 if(used[next.m][next.n]==0) 42 { 43 used[next.m][next.n]=1; 44 q.push(next); 45 } 46 } 47 if(now.n+now.s>n) //s->n 48 { 49 next.m=now.m,next.n=n,next.s=now.n+now.s-n,next.t=now.t+1; 50 if(used[next.m][next.n]==0) 51 { 52 used[next.m][next.n]=1; 53 q.push(next); 54 } 55 } 56 else 57 { 58 next.m=now.m,next.n=now.n+now.s,next.s=0,next.t=now.t+1; 59 if(used[next.m][next.n]==0) 60 { 61 used[next.m][next.n]=1; 62 q.push(next); 63 } 64 } 65 if(now.m+now.n>n) //m->n 66 { 67 next.m=now.m+now.n-n,next.n=n,next.s=now.s,next.t=now.t+1; 68 if(used[next.m][next.n]==0) 69 { 70 used[next.m][next.n]=1; 71 q.push(next); 72 } 73 } 74 else 75 { 76 next.m=0,next.n=now.n+now.m,next.s=now.s,next.t=now.t+1; 77 if(used[next.m][next.n]==0) 78 { 79 used[next.m][next.n]=1; 80 q.push(next); 81 } 82 } 83 if(now.m+now.s>s) //m->s 84 { 85 next.m=now.m+now.s-s,next.n=now.n,next.s=s,next.t=now.t+1; 86 if(used[next.m][next.n]==0) 87 { 88 used[next.m][next.n]=1; 89 q.push(next); 90 } 91 } 92 else 93 { 94 next.m=0,next.n=now.n,next.s=now.m+now.s,next.t=now.t+1; 95 if(used[next.m][next.n]==0) 96 { 97 used[next.m][next.n]=1; 98 q.push(next); 99 } 100 } 101 if(now.n+now.m>m) //n->m 102 { 103 next.m=m,next.n=now.n+now.m-m,next.s=now.s,next.t=now.t+1; 104 if(used[next.m][next.n]==0) 105 { 106 used[next.m][next.n]=1; 107 q.push(next); 108 } 109 } 110 else 111 { 112 next.m=now.n+now.m,next.n=0,next.s=now.s,next.t=now.t+1; 113 if(used[next.m][next.n]==0) 114 { 115 used[next.m][next.n]=1; 116 q.push(next); 117 } 118 } 119 if(now.n+now.s>s) //n->s 120 { 121 next.m=now.m,next.n=now.n+now.s-s,next.s=s,next.t=now.t+1; 122 if(used[next.m][next.n]==0) 123 { 124 used[next.m][next.n]=1; 125 q.push(next); 126 } 127 } 128 else 129 { 130 next.m=now.m,next.n=0,next.s=now.n+now.s,next.t=now.t+1; 131 if(used[next.m][next.n]==0) 132 { 133 used[next.m][next.n]=1; 134 q.push(next); 135 } 136 } 137 } 138 return -1; 139 } 140 int main() 141 { 142 while(cin>>s>>n>>m,n+m+s) 143 { 144 if(s%2) 145 printf("NO\n"); 146 else 147 { 148 if(n<m) swap(n,m); 149 int ans=bfs(); 150 if(ans==-1) 151 printf("NO\n"); 152 else 153 printf("%d\n",ans); 154 } 155 } 156 return 0; 157 }
HDU 2612
链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=15717
题意:两个小伙伴打算在KFC见面。。求两人到KFC最短的路程。KFC的个数>=1
题解:感觉BFS DFS都可以做吧。。我的做法是暴力BFS了。。。要注意其中有的KFC是不能到的。
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 char s[200+5][200+5]; 8 int vis[200+5][200+5]; 9 int tm[200+5][200+5]; 10 int dir[4][2] = {1,0,-1,0,0,1,0,-1}; 11 struct st{ 12 int x,y; 13 int t; 14 }; 15 queue<st>q; 16 void bfs(int x,int y,int n,int m) 17 { 18 memset(vis,0,sizeof(vis)); 19 st f; 20 f.x = x; 21 f.y = y; 22 f.t = 0; 23 q.push(f); 24 vis[f.x][f.y] = 1; 25 while(!q.empty()) 26 { 27 f = q.front(); 28 q.pop(); 29 for( int i = 0;i < 4; i++ ) 30 { 31 st g; 32 g.x = f.x+dir[i][0]; 33 g.y = f.y+dir[i][1]; 34 g.t = f.t+1; 35 if((s[g.x][g.y]=='.'||s[g.x][g.y]=='@')&&!vis[g.x][g.y]&&g.x>=1&&g.x<=n&&g.y>=1&&g.y<=m) 36 { 37 q.push(g); 38 if(s[g.x][g.y]=='@') 39 tm[g.x][g.y]+=g.t; 40 vis[g.x][g.y] = 1; 41 } 42 } 43 } 44 } 45 46 int main() 47 { 48 int n,m; 49 while(cin>>n>>m) 50 { 51 memset(tm,0,sizeof(tm)); 52 memset(s,0,sizeof(s)); 53 int x1,x2,y1,y2; 54 for(int i = 1; i <= n; i++ ) cin>>s[i]+1; 55 for(int i = 1; i <= n; i++ ) 56 for(int j = 1; j <= m; j++ ) 57 { 58 if( s[i][j] == 'Y') {x1 = i; y1 = j;} 59 if( s[i][j] == 'M') {x2 = i; y2 = j;} 60 } 61 bfs(x1,y1,n,m); 62 bfs(x2,y2,n,m); 63 int mi = 999999989; 64 for(int i = 1;i <= n; i++) 65 for(int j = 1; j <= m;j++ ) 66 { 67 if(s[i][j]=='@'&&tm[i][j]!=0) 68 mi=min(mi,tm[i][j]); 69 } 70 printf("%d\n",mi*11); 71 } 72 return 0; 73 }
浙公网安备 33010602011771号