A
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1Sample Output
2 1A题是n皇后问题的变种,不过没有abs(i-j)=abs(a[i]-a[j])的条件,即没有对角线的条件,vis[i]表示搜索过了列,step表示行
用一下dfs就好了,下面的dfs(step+1,m)是在step行没有放棋子的条件,记得回溯就可以做出题目了
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n,k;
int vis[10];
char map[10][10];
int Count;
void dfs(int step,int m)//step为行,m为摆放棋子数
{
int i;
if(m==k)
{
Count++;
return ;
}
if(step==n) return ;
for(i=0; i<=n-1; i++)
{
if(map[step][i]=='#'&&!vis[i])
{
vis[i]=1;
dfs(step+1,m+1);
vis[i]=0;
}
}
dfs(step+1,m);
}
int main()
{
int i;
while(scanf("%d%d",&n,&k))
{
if(n==-1&&k==-1) break;
memset(vis,0,sizeof(vis));
Count=0;
for(i=0; i<=n-1; i++)
{
scanf("%s",map[i]);
}
dfs(0,0);
printf("%d\n",Count);
}
}
B
Is an escape possible? If yes, how long will it take?
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.
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!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0Sample Output
Escaped in 11 minute(s). Trapped!
第一次做三维空间的题目,应该用map[i][j][k]来表示坐标,有六个方向,其他和二维bfs没有什么区别,queue是用front来取头元素,而不是用top,优先队列才是用top!!搞了半天
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char map[40][40][40];
int n,m,t;
int vis[40][40][40];
int dx[6]= {0,0,0,0,1,-1};
int dy[6]= {1,-1,0,0,0,0};
int dz[6]= {0,0,1,-1,0,0};
struct node
{
int x;
int y;
int z;
int step;
};
int pd(int i,int j,int k)
{
if(i<0||i>=t||j<0||j>=n||k<0||k>=m||vis[i][j][k]||map[i][j][k]=='#') return 1;
else return 0;
}
void bfs(int a,int b,int c)
{
queue<node> que;
int t,i,j,k,x,y,z,step;
node top,temp;
temp.x=a;
temp.y=b;
temp.z=c;
temp.step=0;
que.push(temp);
while(!que.empty())
{
top=que.front();
que.pop();
x=top.x;
y=top.y;
z=top.z;
step=top.step;
if(map[x][y][z]=='E')
{
printf("Escaped in %d minute(s).\n",step);
return ;
}
for(t=0; t<=5; t++)
{
i=x+dx[t];
j=y+dy[t];
k=z+dz[t];
if(pd(i,j,k)) continue;
vis[i][j][k]=1;
temp.x=i;
temp.y=j;
temp.z=k;
temp.step=step+1;
que.push(temp);
}
}
printf("Trapped!\n");
}
int main()
{
int i,j,k,a,b,c;
while(scanf("%d%d%d",&t,&n,&m))
{
if(t==0&&n==0&&m==0) break;
memset(vis,0,sizeof(vis));
for(i=0; i<=t-1; i++)
{
for(j=0; j<=n-1; j++)
{
scanf("%s",map[i][j]);
for(k=0; k<=m-1; k++)
{
if(map[i][j][k]=='S')
{
a=i;
b=j;
c=k;
}
}
}
}
bfs(a,b,c);
}
}
C
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?
5 17Sample Output
4Hint
典型bfs题目,注意题目应该有上界的100000,一开始以为上界没用TLE了
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node
{
int x;
int step;
};
int n,m;
int vis[100009];
void bfs()
{
node temp;
int x,step,i,y;
queue<node>q;
temp.x=n;
temp,step=0;
q.push(temp);
while(!q.empty())
{
temp=q.front();
q.pop();
x=temp.x;
step=temp.step;
if(x==m)
{
printf("%d\n",step);
return ;
}
for(i=0; i<=2; i++)
{
if(i==0) y=x-1;
else if(i==1) y=x+1;
else if(i==2) y=x*2;
if(y<0||y>100009||vis[y]) continue;
vis[y]=1;
temp.x=y;
temp.step=step+1;
q.push(temp);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
bfs();
}
}
E
2 6 19 0Sample Output
10 100100100100100100 111111111111111111
bfs题目,两个入口,要么是x*10+0,要么是x*10+1,实际上19位数已经足够了,100位数是坑你的,这个没有必要做vis数组,因为数字一定是增大的,不会重复
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cstring>
#define mod 1000000007
using namespace std;
int n;
void bfs()
{
queue<long long>q;
int i;
long long x;
q.push(1);
while(!q.empty())
{
x=q.front();
if(x%n==0)
{
printf("%lld\n",x);
return ;
}
q.pop();
q.push(x*10);
q.push(x*10+1);
}
}
int main()
{
while(scanf("%d",&n)&&n)
{
bfs();
}
}
F
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— 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
3 1033 8179 1373 8017 1033 1033Sample Output
6 7 0
题目意思是,由n到m,每次只改变一位上的数字,并且改变后的数字一定要是素数,求出最少改变次数。素数打表,40个入口,遍历每一个位数上的数字,个位十位百位千位全部从0-9遍历,找出其中能组成素数的数组入列就可以了
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
int vis[10009];
int p[20000];
struct node
{
int x;
int step;
};
void bfs()
{
int x,y,i,step;
node temp;
queue<node>q;
temp.x=n;
temp.step=0;
q.push(temp);
while(!q.empty())
{
temp=q.front();
q.pop();
x=temp.x;
step=temp.step;
// printf("%d\n",x);
if(x==m)
{
printf("%d\n",step);
return ;
}
for(i=0; i<=9; i++)
{
y=x/10*10+i;//¸öλ
if(!vis[y]&&!p[y])
{
vis[y]=1;
temp.x=y;
temp.step=step+1;
q.push(temp);
}
}
for(i=0; i<=9; i++)
{
y=x/100*100+i*10+x%10;//ʮλ
if(!vis[y]&&!p[y])
{
vis[y]=1;
temp.x=y;
temp.step=step+1;
q.push(temp);
}
}
for(i=0; i<=9; i++)
{
y=x/1000*1000+i*100+x/10%10*10+x%10;
if(!vis[y]&&!p[y])
{
vis[y]=1;
temp.x=y;
temp.step=step+1;
q.push(temp);
}
}
for(i=1; i<=9; i++)
{
y=i*1000+x/100%10*100+x/10%10*10+x%10;
if(!vis[y]&&!p[y])
{
vis[y]=1;
temp.x=y;
temp.step=step+1;
q.push(temp);
}
}
}
}
int main()
{
int i,j,t;
for(i=2;i<=10000;i++)
{
for(j=2*i;j<=10000;j+=i)
{
p[j]=1;
}
}
scanf("%d",&t);
while(t--)
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&m);
bfs();
}
}
G
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.
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 (S1 and 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 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.
2 4 AHAH HAHA HHAAAAHH 3 CDE CDE EEDDCCSample Output
1 2 2 -1
读不懂题系列,其实单纯模拟就可以了,两个数组相互插入,然后组成新的数组是否和给定数组相同,如果不同,继续上一步的操作,求出最少操作次数,如果不可以即循环,输出-1,注意一下题目的数组要加入结束符,否则可能因为上一次的数组而导致输出错误。
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cstring>
#define mod 1000000007
using namespace std;
char s[100009];
int main()
{
int i,j,k,t,n;
scanf("%d",&t);
char s1[109],s2[109],c[209],s[209],temp1[109],temp2[109];
int Count;
int T=1;
while(t--)
{
Count=0;
scanf("%d",&n);
scanf("%s%s%s",s1,s2,s);
strcpy(temp1,s1);
strcpy(temp2,s2);
while(1)
{
Count++;
for(i=0,j=0,k=0; k<=2*n-1; k++)
{
if(k%2) c[k]=s1[i++];
else c[k]=s2[j++];
}
c[k]='\0';//假如没有结束符,可能上一次的数组会使得它出错
if(strcmp(c,s)==0)
{
printf("%d %d\n",T++,Count);
break;
}
else
{
for(i=0; i<=n-1; i++)
{
s1[i]=c[i];
s2[i]=c[i+n];
}
//printf("%s\n%s\n",s1,s2);
if(strcmp(temp1,s1)==0&&strcmp(temp2,s2)==0)
{
printf("%d -1\n",T++);
break;
}
}
}
}
}