Write a program to count the number of black tiles which he can reach by repeating the moves described above.
InputThe input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
OutputFor each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13
思路:这道题我参考了书本然后用了bfs,在这个矩阵中,点只能往四个方向走,分别是上下左右,所以设置数组dir来实现坐标的变化,check则是判断这个点有没有超出矩阵的大小,结构体就是为了存储这个点的坐标而建立的。
然后是bfs函数,因为题目出发的点也算一个,所以sum初始值为1,然后建立队列,把第一个点压入队列,开始循环一直到队列为空(也就是把矩阵都访问一遍)。对每一个点都进行四次循环,如果周围的点中有可以走的点,num加1,然后把这个点标记为不可走,并把这个点压入队列。如此循环往复,直到队列为空。
ac代码:
#include <iostream> #include <queue> using namespace std; char room[23][23]; int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; int wx,hy,num; #define check(x,y)(x<wx&&x>=0&&y>=0&&y<hy) struct node{int x,y;}; void bfs(int dx,int dy){ num=1; queue<node>q; node start,next; start.x=dx; start.y=dy; q.push(start); while(!q.empty()){ start=q.front(); q.pop(); for(int i=0;i<4;i++){ next.x=start.x+dir[i][0]; next.y=start.y+dir[i][1]; if(check(next.x,next.y)&&room[next.x][next.y]=='.'){ room[next.x][next.y]='#'; num++; q.push(next); } } } } int main(int argc, const char * argv[]) { int x,y,dx,dy; while(cin>>wx>>hy){ if(wx==0&&hy==0){ break; } for(y=0;y<hy;y++){ for(x=0;x<wx;x++){ cin>>room[x][y]; if(room[x][y]=='@'){ dx=x; dy=y; } } } num=0; bfs(dx,dy); cout<<num<<endl; } return 0; }
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
#include <iostream> #include <queue> using namespace std; int bfs(int n,int k){ int flag1[1000001]; int bs[1000001]; queue<int>q; q.push(n); int start=0; if(n==k){ return 0; } while(!q.empty()){ start=q.front(); q.pop(); if(start+1<1000001&&flag1[start+1]!=1){ flag1[start+1]=1; bs[start+1]=bs[start]+1; q.push(start+1); } if(start+1==k) break; if(start-1>=0&&flag1[start-1]!=1){ flag1[start-1]=1; bs[start-1]=bs[start]+1; q.push(start-1); } if(start-1==k) break; if(start*2<1000001&&flag1[start*2]!=1){ flag1[start*2]=1; bs[start*2]=bs[start]+1; q.push(start*2); } if(start*2==k) break; } return bs[k]; } int main(int argc, const char * argv[]) { int N,K; cin>>N>>K; cout<<bfs(N, K)<<endl; return 0; }
Input
Output
Sample Input
2 6 19 0
Sample Output
10 100100100100100100 111111111111111111
思路:这道题要找一个数能够整除题目中给的数,这道题目也只有两个方向,从1开始,要不就是*10,要不就是*10+1,利用bfs遍历输出其中最小的一个就行了
ac代码:
#include <iostream> #include <queue> using namespace std; void bfs(long long n,int m){ queue<long long>q; q.push(n); while(!q.empty()){ long long t=q.front(); q.pop(); if(t%m==0){ cout<<t<<endl; return ; } else {q.push(t*10);q.push(t*10+1);} } } int main(int argc, const char * argv[]) { int k; bool flag1=true; while(flag1){ cin>>k; if(k==0){ flag1=false; break; } if(k!=0){ bfs(1,k); } } return 0; }
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
1733
3733
3739
3779
8779
8179
Input
Output
Sample Input
3
思路;这道题目就是将一个质数通过最少的步骤转换成另外一个步数,换一个数字要➕1,然后主要是四个方向的变化,分别为个十百千位的变化,但是要注意的是个位必须是奇数,千位要大于等于1,然后我不会写质数表,就写了一个判断它是不是质数的函数,然后利用bfs就可以解决了
ac代码:
#include <iostream> #include <string.h> #include <cmath> #include <algorithm> #include <queue> using namespace std; #define max 1000000 int bool1[max]; struct node{ int data; int step; }x,y; bool judge(int x)//判断是否为素数// { if(x==1||x==0) return 0; for(int i = 2; i <= sqrt(x); i ++) { if(x % i == 0) return 0; } return 1; } //变化个位:X/10*10+i;变化十位:X/100*100+i*10+X%10;变化百位:X/1000*1000+i*100+i%100;变化千位:i*1000+i%1000;// void bfs(int n,int m){ queue<node>q; memset(bool1,0,sizeof(bool1)); bool1[n]=1; x.data=n; x.step=0; q.push(x); while(!q.empty()){ x=q.front(); q.pop(); if(x.data==m){ cout<<x.step<<endl; return; } //个位// for(int i=1;i<=9;i=i+2){ int k=x.data/10*10+i; if(judge(k)&&!bool1[k]){ bool1[k]=1; y.data=k; y.step=x.step+1; q.push(y); } } //十位// for(int i=0;i<=9;i++){ int t=x.data%10; int k=x.data/100*100 +i*10+t; if(judge(k)&&!bool1[k]){ bool1[k]=1; y.data=k; y.step=x.step+1; q.push(y); } } //百位// for(int i=0;i<=9;i++){ int t=x.data%100; int k=x.data/1000*1000 +i*100+t; if(judge(k)&&!bool1[k]){ bool1[k]=1; y.data=k; y.step=x.step+1; q.push(y); } } //千位// for(int i=1;i<=9;i++){ int t=x.data%1000; int k=i*1000+t; if(judge(k)&&!bool1[k]){ bool1[k]=1; y.data=k; y.step=x.step+1; q.push(y); } } } return; } int main(int argc, const char * argv[]) { int num; int k,l; cin>>num; while(num--){ cin>>k>>l; bfs(k,l); } return 0; }
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
Output
Sample Input
10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W.
Sample Output
3
Hint
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
#include <iostream> #define max 107; using namespace std; int dir[8][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{1,-1},{-1,1}}; char a[107][107]; int n,m; void dfs(int x,int y){ a[x][y]='.'; for(int i=0;i<8;i++){ int x1,y1; x1 = x+dir[i][0]; y1 = y+dir[i][1]; if(x1>n-1||x1<0||y1<0||y1>m-1){ continue; } if(a[x1][y1]=='.'){ continue; } dfs(x1,y1); } } int main(int argc, const char * argv[]){ while(cin>>n>>m){ int sum=0; if(n==0||m==0){ break; } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>a[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(a[i][j]=='W'){ dfs(i, j); cout<<i<<j<<endl; sum=sum+1; } } } cout<<sum; } return 0; }