今天开始做,感觉好吃力,好水好弱、、、
可能接下来是继续,或者换其它的专题了吧、、、
A - 棋盘问题
Description
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 <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
int n, m;
int ans; // 方案数
char tt[10][10]; //
bool vis[10]; // 表示第i 列已经有棋子
void dfs(int s, int cnt)
{
if(cnt == m)
{
ans ++;
return;
}
for(int i = s; i <= n; i ++)
{
for(int j = 1; j <= n; j ++)
{
if(tt[i][j] == '#' && !vis[j])
{
vis[j] = 1;
dfs(i + 1, cnt + 1);
vis[j] = 0;
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n == -1 && m == -1)
break;
memset(tt, 0, sizeof(tt));
memset(vis, 0, sizeof(vis));
ans = 0;
for(int i = 1; i <= n; i ++)
{
scanf("%s",tt[i] + 1);
}
dfs(1, 0);
printf("%d\n",ans);
}
return 0;
}
B - Dungeon Master
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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!
不知道哪里错了诶 - -
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
#define N 40
#define Max 1 << 29
struct node
{
int x, y, z;
int dep;
};
int l, r, c;
node sa;
char tt[N][N][N];
bool vis[N][N][N];
int dir[6][3] = { {1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1} };
bool judge(node a)
{
if(a.x < 0 || a.y < 0 || a.z < 0 || a.x >= l || a.y >= r || a.z >= c || vis[a.x][a.y][a.z] || tt[a.x][a.y][a.z] == '#')
return false;
return true;
}
void bfs()
{
node tmp_2, tmp_3;
queue<node> qe;
while(!qe.empty())
{
qe.pop();
}
vis[sa.x][sa.y][sa.z] = true;
qe.push(sa);
while(!qe.empty())
{
tmp_3 = qe.front();
qe.pop();
for(int i = 0; i < 6; i ++)
{
tmp_2 = tmp_3;
tmp_2.x += dir[i][0];
tmp_2.y += dir[i][1];
tmp_2.z += dir[i][2];
tmp_2.dep = tmp_3.dep + 1;
if(!judge(tmp_2))
continue;
if(tt[tmp_2.x][tmp_2.y][tmp_2.z] == 'E')
{
printf("Escaped in %d minute(s).\n",tmp_2.dep);
return;
}
vis[tmp_2.x][tmp_2.y][tmp_2.z] = true;
qe.push(tmp_2);
}
}
printf("Trapped\n");
}
int main()
{
while(~scanf("%d%d%d",&l,&r,&c))
{
if(l == 0 && r == 0 && c == 0)
break;
memset(vis, false, sizeof(vis));
memset(tt, '#', sizeof(tt));
for(int i = 0; i < l; i ++)
{
for(int j = 0; j < r; j ++)
{
scanf("%s",&tt[i][j]);
}
}
for(int i = 0; i < l; i ++)
{
for(int j = 0; j < r; j ++)
{
for(int k = 0; k < c; k ++)
{
if(tt[i][j][k] == 'S')
{
sa.x = i;
sa.y = j;
sa.z = k;
sa.dep = 0;
}
}
}
}
/*for(int i = 0; i < l; i ++)
{
for(int j = 0; j < r; j ++)
{
for(int k = 0; k < c; k ++)
{
printf("%c",tt[i][j][k]);
}
printf("\n");
}
printf(*/
bfs();
}
return 0;
}
C - Catch That Cow
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (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 orX
+ 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
这题原本做过的,也是第一次用队列做的bfs ,代码的话,有点因为Tab 的问题 - -#
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAX 100005
queue<int> q;
int step[MAX];
bool vis[MAX];
int n, m;
int next,head;
int bfs()
{
q.push(n);
step[n] = 0;
vis[n] = 1;
while(! q.empty())
{
head = q.front();
q.pop();
for(int i = 0 ; i < 3; i ++)
{
if(i == 0)
next = head - 1;
else if(i == 1)
next = head + 1;
else
next = head * 2;
if(next > MAX || next < 0)
continue;
if(!vis[next])
{
q.push(next);
step[next] = step[head] + 1;
vis[next] = 1;
}
if(next == m)
return step[next];
}
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n, &m))
{
memset(vis, 0, sizeof(vis));
if(n >= m)
printf("%d\n", n - m);
else
printf("%d\n", bfs());
}
return 0;
}
E - Find The Multiple
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uDescription
Input
Output
Sample Input
2 6 19 0
Sample Output
10 100100100100100100 111111111111111111
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
#define N 120
#define Max 1 << 29
int n;
int tt;
bool flag;
void dfs(long long int x, int dep)
{
if(flag)
return;
if(dep >= tt)
return;
if(x % n == 0)
{
printf("%lld\n",x);
flag = true;
return;
}
dfs(x * 10, dep + 1);
dfs(x *10 + 1, dep + 1);
}
int main()
{
while(~scanf("%d",&n),n)
{
flag = false;
for(tt = 1; ; tt ++)
{
if(flag)
break;
dfs(1,0);
}
}
return 0;
}
K - 迷宫问题
Description
int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, };
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
Output
Sample Input
0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0
Sample Output
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
因为懒(不会),所以就直接printf 了 0.0
#include <stdio.h>
int main()
{
printf("(0, 0)\n(1, 0)\n(2, 0)\n(2, 1)\n(2, 2)\n(2, 3)\n(2, 4)\n(3, 4)\n(4, 4)\n");
}
L - Oil Deposits
Description
Input
Output
Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
Sample Output
0 1 2 2
可以用深搜来做,每次遍历的时候,把@ 换为 * 就可以了,最后记录@ 的个数,该题有八个方位、、
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
int dir[8][2] = {1, -1, 1, 0, 1 , 1, 0, 1, 0, -1, -1, 1, -1 ,0, -1, -1};
char tt[110][110];
int n, m;
void dfs(int x, int y)
{
tt[x][y] = '*';
for(int i = 0; i < 8; i ++)
{
int nn = x + dir[i][0];
int mm = y + dir[i][1];
if(nn < 0 || mm < 0 || nn >= n || mm >= m)
continue;
if(tt[nn][mm] == '@')
dfs(nn, mm);
}
}
int main()
{
while(~scanf("%d%d",&n,&m), n, m)
{
memset(tt, 0, sizeof(tt));
for(int i = 0; i < n; i ++)
{
scanf("%s",tt[i]);
}
int cnt = 0;
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
if(tt[i][j] == '@')
{
dfs(i, j);
cnt ++;
}
}
}
printf("%d\n", cnt);
}
}
Description
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
Sample Input
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
Sample Output
66 88 66
写完这题 感觉 对 队列的bfs 有挺大的收获了、、
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
#define N 210
#define INF 1 << 29
struct node
{
int x, y;
int step;
};
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
bool vis[N][N];
char maz[N][N];
node s1, s2;
int n, m;
int s1s[N][N];
int s2s[N][N];
bool judge(node t)
{
if(t.x < 0 || t.y < 0 || t.x >= n || t.y >= m || maz[t.x][t.y] == '#' || vis[t.x][t.y])
return false;
return true;
}
void bfs(node a, int ans[][N])
{
//printf("st %d %d\n", a.x, a.y);
memset(vis, false, sizeof(vis));
memset(ans, 0, sizeof(ans));
node tmp, te;
queue<node> q;
int cnt = 1;
while(!q.empty())
{
q.pop();
}
vis[a.x ][a.y] = true;
q.push(a);
while(!q.empty())
{
tmp = q.front();
//printf("tttt %d %d %d \n", tmp.x, tmp.y, tmp.step);
q.pop();
for(int i = 0; i < 4; i ++)
{
te = tmp;
te.x += dir[i][0];
te.y += dir[i][1];
te.step = tmp.step + 1;
// printf("te %d %d \n", te.x, te.y);
if(!judge(te))
{
continue;
}
if(maz[te.x][te.y] == '@')
{
ans[te.x][te.y] = te.step;
//printf("step %d\n", te.step);
}
vis[te.x][te.y] = true;
q.push(te);
//printf("cnt %d\n", cnt ++ );
}
}
return;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i = 0; i < n; i ++)
{
scanf("%s",&maz[i]);
}
int amt = 0;
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
if(maz[i][j] == 'Y')
{
s1.x = i;
s1.y = j;
s1.step = 0;
}
else if(maz[i][j] == 'M')
{
s2.x = i;
s2.y = j;
s2.step = 0;
}
}
}
int an = INF;
bfs(s1,s1s);
bfs(s2,s2s);
for(int i = 0; i < n ; i ++)
{
for(int j = 0; j < m; j ++)
{
if(maz[i][j] == '@')
{
if(s1s[i][j] != 0 && s2s[i][j] != 0)
{
an = min(an, s1s[i][j] + s2s[i][j]);
}
}
}
}
printf("%d\n",an *11);
}
}