Links:https://vjudge.net/contest/146840#overview
A - 棋盘问题
比较简单的搜索题
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
int n, k, ans;
char map[10][10];
bool line[10];
void dfs(int lie, int num){
if (num == k){
ans++;
return;
}
if (lie == n){
return;
}
dfs(lie + 1, num);
for (int i = 0; i<n; i++){
if (map[lie][i] == '#'&&line[i] == false){
line[i] = 1;
dfs(lie + 1, num + 1);
line[i] = 0;
}
}
}
int main(){
while (~scanf("%d%d", &n, &k), n != -1){
ans = 0;
memset(line, 0, sizeof(line));
for (int i = 0; i<n; i++){
cin >> map[i];
}
dfs(0, 0);
printf("%d\n", ans);
}
return 0;
}
B - Dungeon Master
二维广搜变成三维广搜,没什么其他不同
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<queue>
using namespace std;
int l, r, c,ans;
int map[32][32][32];
bool v[32][32][32];
int dl[6] = { 0, 0, 0, 0, 1, -1 };
int dx[6] = { -1, 0, 0, 1, 0, 0 };
int dy[6] = { 0, -1, 1, 0, 0, 0 };
struct point{
int l;
int x;
int y;
int s;
}beg;
queue<point> que;
int bfs(){
int ans = -1;
while (!que.empty()){
point cnt = que.front();
que.pop();
if (map[cnt.l][cnt.x][cnt.y]=='E'){ ans = cnt.s; break; }
for (int i = 0; i < 6; i++){
point np={ cnt.l + dl[i], cnt.x + dx[i], cnt.y + dy[i], cnt.s + 1 };
if (np.l >= 0 && np.l<l&&np.x>=0 && np.x<r&&np.y>=0 && np.y < c){
if (map[np.l][np.x][np.y] != '#'&&v[np.l][np.x][np.y]==0){
que.push(np);
v[np.l][np.x][np.y] = 1;
}
}
}
}
while (!que.empty()){
que.pop();
}
return ans;
}
int main(){
while (scanf("%d%d%d", &l, &r, &c)){
memset(v, 0, sizeof(v));
getchar();
if (l == 0){ return 0; }
for (int i = 0; i < l; i++){
for (int j = 0; j < r; j++){
for (int k = 0; k < c; k++){
map[i][j][k]=getchar();
if (map[i][j][k] == 'S'){
beg.l = i, beg.x = j, beg.y = k,beg.s=0;
v[i][j][k] = 1;
}
}
getchar();
}
getchar();
}
que.push(beg);
int ans = bfs();
if (ans == -1){
printf("Trapped!\n");
}
else{
printf("Escaped in %d minute(s).\n",ans);
}
}
return 0;
}
C - Catch That Cow
数轴上的bfs(),给定几个移动,求最少步骤达到要求的位置。
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<queue>
using namespace std;
int fx, fy;
int v[300005];
struct point{
int x;
int s;
}beg;
queue<point> que;
int bfs(){
int ans = -1;
while (!que.empty()){
point cnt = que.front();
que.pop();
if (cnt.x == fy){
return cnt.s;
}
if (cnt.x + 1 <= 100000&&v[cnt.x + 1] == 0){
que.push({ cnt.x + 1, cnt.s + 1 });
v[cnt.x + 1] = 1;
}
if (cnt.x - 1 >= 0 && v[cnt.x - 1] == 0){
que.push({ cnt.x - 1, cnt.s + 1 });
v[cnt.x - 1] = 1;
}
if (cnt.x != 0 &&cnt.x*2<=300000&& v[cnt.x * 2] == 0){
que.push({ cnt.x * 2, cnt.s + 1 });
v[cnt.x * 2] = 1;
}
}
return ans;
}
int main(){
scanf("%d%d", &fx, &fy);
beg.x = fx, beg.s = 0;
que.push(beg);
int astep=bfs();
printf("%d", astep);
return 0;
}
D - Fliptile
经典的翻牌子,枚举搜索。
E - Find The Multiple
着给定数字的倍数中全为0.1的,一开始看位数最多为200认为dfs会超时,然后就用了广搜,结果爆内存,最后打了表过的,其实答案用longlong就能存的下,深搜就能解决。
mle的广搜代码
#include<iostream>