这个章节的内容就是利用bfs,dfs进行搜索。简单搜索,内容不是很难。
A - 棋盘问题 POJ - 1321
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2 1
题解:不能再同一行同一列,就像八皇后那题一样,以行为基础,每次选择列的
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int maxn=15;
int n,k;
char s[maxn][maxn];
bool vis[maxn];
int ans;
void dfs(int x,int cnt)
{
if(cnt>=k)
{
ans++;
return;
}
for(int i=x;i<n;i++)
{
for(int j=0;j<=n;j++)
{
if(s[i][j]=='#'&&vis[j]==false)
{
vis[j]=true;
dfs(i+1,cnt+1);
vis[j]=false;//每次选择了,后面要记得消除标记
}
}
}
}
int main()
{
while(scanf("%d %d",&n,&k)!=EOF)
{
if(n==-1&&k==-1)
break;
memset(vis,false,sizeof(vis));
ans=0;
for(int i=0;i<n;i++)
scanf("%s",s[i]);
dfs(0,0);
printf("%d
",ans);
}
return 0;
}
B - Dungeon Master POJ - 2251
Is an escape possible? If yes, how long will it take?
Input
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
题解:给一个三维图,可以前后左右上下6种走法,走一步1分钟,求最少时间。简单的bfs跑一遍,注意好边界的判断就行了。
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=35;
int l,r,c;
int ans;
bool flag;
int sx,sy,sz;
char s[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};
struct Node
{
int x,y,z;
int step;
};
Node now,temp,nextt;
void bfs()
{
int x,y,z;
queue<Node>q;
vis[sz][sx][sy]=1;
now.x=sx;
now.y=sy;
now.z=sz;
now.step=0;
q.push(now);
while(!q.empty())
{
temp=q.front();
q.pop();
for(int i=0;i<6;i++)
{
x=nextt.x=temp.x+dx[i];
y=nextt.y=temp.y+dy[i];
z=nextt.z=temp.z+dz[i];
nextt.step=temp.step+1;
if(x<0||x>=r||y<0||y>=c||z<0||z>=l)//判断边界
continue;
if(s[z][x][y]=='#')
continue;
if(s[z][x][y]=='E')//到终点跳出。
{
ans=nextt.step;
flag=true;
return;
}
if(s[z][x][y]=='.'&&vis[z][x][y]==false)
{
vis[z][x][y]=true;
q.push(nextt);
}
}
}
}
int main()
{
int i,j,k;
while(scanf("%d %d %d",&l,&r,&c)!=EOF)
{
if(l==0&&r==0&&c==0)
break;
memset(vis,false,sizeof(vis));
flag=false;
for(i=0;i<l;i++)
{
for(j=0;j<r;j++)
{
scanf("%s",s[i][j]);
for(k=0;k<c;k++)
{
if(s[i][j][k]=='S')
{
sx=j;sy=k;sz=i;
}
}
}
}
bfs();
if(flag)
printf("Escaped in %d minute(s).
",ans);
else
printf("Trapped!
");
}
return 0;
}
C - Catch That Cow POJ - 3278
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
题解:给定两个整数n和k通过 n+1或n-1 或n*2 这3种操作,使得n==k,输出最少的操作次数
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100005;
struct Node
{
int pos;
int step;
};
queue<Node>q;
bool vis[maxn];
int n,k;
void bfs()
{
Node now,temp,nextt;
now.pos=n;now.step=0;
q.push(now);
vis[now.pos]=true;
while(!q.empty())
{
temp=q.front();
q.pop();
if(temp.pos==k)
{
printf("%d
",temp.step);
return;
}
else
{
nextt.pos=temp.pos-1;
if(nextt.pos>=0&&vis[nextt.pos]==false)//向后退,但是注意一定要大于0
{
vis[nextt.pos]=true;
nextt.step=temp.step+1;
q.push(nextt);
}
nextt.pos=temp.pos+1;
if(nextt.pos<=maxn&&vis[nextt.pos]==false)//向前进,但注意要小于maxn
{
vis[nextt.pos]=true;
nextt.step=temp.step+1;
q.push(nextt);
}
nextt.pos=temp.pos*2;
if(nextt.pos<=maxn&&vis[nextt.pos]==false)//向前跳,但是要小于maxn
{
vis[nextt.pos]=true;
nextt.step=temp.step+1;
q.push(nextt);
}
}
}
}
int main()
{
memset(vis,false,sizeof(vis));
scanf("%d %d",&n,&k);
if(n>=k)//如果在牛的后面,那么一直向后退就是最优的走法。
printf("%d
",n-k);
else
bfs();
return 0;
}
E - Find The Multiple POJ - 1426
Input
Output
Sample Input
2
6
19
0
Sample Output
10 100100100100100100 111111111111111111
题意:一个数,只有01组成,找到能够整除n的值。那么对于每个位枚举0,1一直跑,每次看能否整除。最后得到的结果就行了。注意要开long long
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=100005;
int n;
void bfs()
{
queue<LL>q;//注意开long long
q.push(1);//不可能以0开头
LL temp;
while(!q.empty())
{
temp=q.front();
q.pop();
if(temp%n==0)
{
printf("%lld
",temp);
return;
}
q.push(temp*10);
q.push(temp*10+1);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
bfs();
}
return 0;
}
F - Prime Path POJ - 3126
— 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
1033 8179
1373 8017
1033 1033
Sample Output
6 7 0
题意:给定两个素数a b,求a变幻到b需要几步
并且变幻时只有一个数字不同,并且是素数,那么就是对于每一位上的数字进行变化,如果是素数才入队列,
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <math.h>
using namespace std;
const int maxn=100005;
int T,n,m;
bool vis[maxn];
struct Node
{
int x;
int step;
};
bool judge_prime(int n)//判断是否为素数,这是之前写的代码,其实建议先用线性筛筛出素数打表,复杂度会低一些。
{
if(n==1)
return 0;
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
return false;
}
return true;
}
void bfs()
{
memset(vis,0,sizeof(vis));
Node now,temp,nextt;
now.x=n;
now.step=0;
vis[now.x]=1;
queue<Node>q;
q.push(now);
while(!q.empty())
{
temp=q.front();
q.pop();
if(temp.x==m)
{
printf("%d
",temp.step);
return ;
}
for(int d=0;d<4;d++)
{
if(d==0)//个位
{
for(int i=1;i<=9;i+=2)//个位上偶数就不可能是素数,所以是+2
{
nextt=temp;
nextt.x=(temp.x)/10*10+i;
if(nextt.x!=temp.x&&!vis[nextt.x]&&judge_prime(nextt.x))
{
vis[nextt.x]=1;
nextt.step=temp.step+1;
q.push(nextt);
}
}
}
else if(d==1)//十位
{
for(int i=0;i<=9;i++)
{
nextt=temp;
nextt.x=temp.x/100*100+i*10+temp.x%10;
if(nextt.x!=temp.x &&!vis[nextt.x]&&judge_prime(nextt.x))
{
vis[nextt.x]=1;
nextt.step=temp.step+1;
q.push(nextt);
}
}
}
else if(d==2)//百位
{
for(int i=0;i<=9;i++)
{
nextt=temp;
nextt.x=temp.x/1000*1000+i*100+temp.x%100;
if(nextt.x!=temp.x &&!vis[nextt.x]&&judge_prime(nextt.x))
{
vis[nextt.x]=1;
nextt.step=temp.step+1;
q.push(nextt);
}
}
}
else if(d==3)
{
for(int i=1;i<=9;i++)
{
nextt.x=i*1000+temp.x%1000;
if(nextt.x!=temp.x&&!vis[nextt.x]&&judge_prime(nextt.x))
{
vis[nextt.x]=1;
nextt.step=temp.step+1;
q.push(nextt);
}
}
}
}
}
printf("Impossible
");
return ;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
bfs();
}
return 0;
}
G - Shuffle'm Up POJ - 3087
A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several different colors.
The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:
The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.
After the shuffle operation, S12 is split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle operation may then be repeated to form a new S12.
For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.
Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.
Output
Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.
Sample Input
2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
Sample Output
1 2 2 -1
题意:其实这一题,我觉得直接就是模拟,并不需要搜索,按照题意一步步的变化,进行比较,但是要利用stl的map来记录这个string是否已经出现过了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=205;
const int INF=0x3f3f3f3f;
int T,n,m;
char s1[maxn],s2[maxn];
char s[maxn*2];
map<string,bool>vis;
int main()
{
scanf("%d",&T);
int casee=1;
while(T--)
{
scanf("%d",&n);
scanf("%s",s1);//输入两个字符串
scanf("%s",s2);
scanf("%s",s);
vis[s]=true;
int step=0;
while(1)
{
char temp[maxn*2];
int cnt=0;
for(int i=0;i<n;i++)//两个字符串合并
{
temp[cnt++]=s2[i];
temp[cnt++]=s1[i];
}
temp[cnt]='