棋盘问题
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int MN = 505;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
int n, k;
char s[10][10];
int vis[10];
int ans;
void dfs(int x, int t) {
if (t == k) {
ans++;
return ;
}
if (x == n + 1) {
return ;
}
for (int i = 1; i <= n; i++) {
if (vis[i] == 0 && s[x][i] == '#') {
vis[i] = 1;
dfs(x + 1, t + 1);
vis[i] = 0;
}
}
dfs(x + 1, t);
}
int main() {
while (~scanf("%d %d", &n, &k)) {
if (n == -1 && k == -1) {
break;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf(" %c", *(s + i) + j);
}
}
ans = 0;
memset(vis, 0, sizeof(vis));
dfs(1, 0);
printf("%d\n", ans);
}
return 0;
}
Dungeon Master
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int MN = 505;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
int L, R, C;
int ans;
int vis[35][35][35];
char s[35][35][35];
struct step {
int x, y, c, sp;
step(int tx, int ty, int tc, int ts): x(tx), y(ty), c(tc), sp(ts) {
}
};
int gox[6] = {0, 1, 0, 0, 0, -1};
int goy[6] = {1, 0, 0, 0, -1, 0};
int goc[6] = {0, 0, 1, -1, 0, 0};
int ec, ex, ey;
int bfs(int c, int x, int y) {
queue<step> que;
que.push(step(x, y, c, 0));
while (!que.empty()) {
step p = que.front();
que.pop();
//printf("%d %d %d %d\n", p.x, p.y, p.c, p.sp);
if (p.x == ex && p.y == ey && p.c == ec) {
return p.sp;
}
int tx, ty, tc;
for (int i = 0; i <= 5; i++) {
tx = p.x + gox[i];
ty = p.y + goy[i];
tc = p.c + goc[i];
if (tx >= 1 && tx <= R && ty >= 1 && ty <= C && tc >= 1 && tc <= L && s[tc][tx][ty] != '#' && vis[tc][tx][ty] == 0) {
vis[tc][tx][ty] = 1;
que.push(step(tx, ty, tc, p.sp + 1));
//printf();
}
}
}
return 0;
}
void solve() {
ans = 0;
memset(vis, 0, sizeof(vis));
int tx, ty, tc;
for (int i = 1; i <= L; i++) {
for (int j = 1; j <= R; j++) {
for (int k = 1; k <= C; k++) {
scanf(" %c", &s[i][j][k]);
if (s[i][j][k] == 'S') {
tc = i;
tx = j;
ty = k;
}
if (s[i][j][k] == 'E') {
ec = i;
ex = j;
ey = k;
}
}
}
}
int res = bfs(tc, tx, ty);
if (res) {
printf("Escaped in %d minute(s).\n", res);
} else {
printf("Trapped!\n");
}
}
int main() {
while (~scanf("%d %d %d", &L, &R, &C)) {
if (L == 0 && R == 0 && C == 0) {
break;
}
solve();
}
return 0;
}
Catch that cow
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int MN = 505;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
int vis[MAXN];
int N, K;
struct node {
int x, cost;
node(int tx, int tcost): x(tx), cost(tcost) {
}
};
int solve() {
queue<node> que;
que.push(node(N, 0));
while (!que.empty()) {
node t = que.front();
que.pop();
//printf("%d %d\n", t.x, t.cost);
if (t.x == K) {
return t.cost;
}
if (t.x - 1 >= 0 && !vis[t.x - 1]) {
vis[t.x - 1] = 1;
que.push(node(t.x - 1, t.cost + 1));
}
if (t.x + 1 <= 100000 && !vis[t.x + 1]) {
vis[t.x + 1] = 1;
que.push(node(t.x + 1, t.cost + 1));
}
if (t.x * 2 <= 100000 && !vis[t.x * 2]) {
vis[t.x * 2] = 1;
que.push(node(t.x * 2, t.cost + 1));
}
}
}
int main() {
scanf("%d %d", &N, &K);
printf("%d", solve());
return 0;
}
Find The Multiple
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);
int m;
int flag;
void dfs(int step, unsigned long long x) {
if (step > 19 || flag == 1) {
return ;
}
if (x % m == 0) {
flag = 1;
printf("%lld\n", x);
return;
}
dfs(step + 1, x * 10);
dfs(step + 1, x * 10 + 1);
}
int main() {
while (scanf("%d", &m) && m) {
dfs(1, 1);
flag = 0;
}
return 0;
}
Prime Path
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);
const int N = 10005;
bool isprime[N];
int a, b;
typedef pair<int, int> P;
int vis[N];
void sieve() {
for (int i = 0; i <= N; i++) {
isprime[i] = true;
}
isprime[0] = isprime[1] = 0;
for (int i = 2; i <= N; i++) {
if (isprime[i]) {
//printf("%d\t", i);
for (int j = 2 * i; j <= N; j += i) {
isprime[j] = false;
}
}
}
}
inline int getnum(int n4, int n3, int n2, int n1) {
return n4 * 1000 + n3 * 100 + n2 * 10 + n1;
}
bool check(int n4, int n3, int n2, int n1) {
int tmp = getnum(n4, n3, n2, n1);
if (tmp >= 1000 && isprime[tmp] && vis[tmp] == 0) {
return true;
}
return false;
}
void bfs() {
queue<P> que;
que.push(P(a, 0));
vis[a] = 1;
while (!que.empty()) {
P k = que.front();
que.pop();
//printf("%d %d\n", k.first, k.second);
if (k.first == b) {
printf("%d\n", k.second);
return ;
}
int n1 = k.first % 10;
int n2 = k.first / 10 % 10;
int n3 = k.first / 100 % 10;
int n4 = k.first / 1000;
for (int i = 0; i <= 9; i++) {
int num;
if (check(n4, n3, n2, i)) {
num = getnum(n4, n3, n2, i);
vis[num] = 1;
que.push(P(num, k.second + 1));
}
if (check(n4, n3, i, n1)) {
num = getnum(n4, n3, i, n1);
vis[num] = 1;
que.push(P(num, k.second + 1));
}
if (check(n4, i, n2, n1)) {
num = getnum(n4, i, n2, n1);
vis[num] = 1;
que.push(P(num, k.second + 1));
}
if (check(i, n3, n2, n1)) {
num = getnum(i, n3, n2, n1);
vis[num] = 1;
que.push(P(num, k.second + 1));
}
}
}
printf("Impossible\n");
}
int main() {
int t;
scanf("%d", &t);
sieve();
while (t--) {
scanf("%d %d", &a, &b);
bfs();
memset(vis, 0, sizeof(vis));
}
return 0;
}
Shuffle'm Up
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);
int pos = 1;
void solve() {
int len;
scanf("%d", &len);
string s1, s2, s3, s, ss;
cin >> s1 >> s2 >> s;
ss = s1 + s2;
int cnt = 0;
while (1) {
s3 = "";
for (int i = 0; i < len; i++) {
s3 = s3 + s2[i];
s3 = s3 + s1[i];
}
cnt++;
//cout << s3 << endl;
if (s3 == s) {
printf("%d %d\n", pos++, cnt);
return ;
}
for (int i = 0; i < len; i++) {
s1[i] = s3[i];
s2[i] = s3[i + len];
}
//cout << s1 << " " << s2 << endl;
if (ss == s1 + s2) {
printf("%d -1\n", pos++);
return ;
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
Fire!
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);
int R, C;
char s[1010][1010];
int fire[1010][1010];
int vis[1005][1005];
int go[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int sx, sy;
//int fx, fy;
typedef pair<int, int> P;
int pos = 0;
struct ff {
int x, y;
ff() {
}
ff(int tx, int ty): x(tx), y(ty) {
}
} ;
ff Fire[1005];
void firetomap() {
memset(fire, INF, sizeof(fire));
queue<ff> que;
for (int i = 0; i < pos; i++) {
que.push(ff(Fire[i].x, Fire[i].y));
fire[Fire[i].x][Fire[i].y] = 0;
}
while (!que.empty()) {
ff t = que.front();
que.pop();
for (int i = 0; i <= 3; i++) {
int tx = t.x + go[i][0];
int ty = t.y + go[i][1];
if (tx >= 1 && tx <= R && ty >= 1 && ty <= C && s[tx][ty] != '#' && fire[tx][ty] == INF) {
fire[tx][ty] = fire[t.x][t.y] + 1;
que.push(ff(tx, ty));
}
}
}/*
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
printf("%d ", fire[i][j]);
}
printf("\n");
}*/
}
struct node {
int x, y, time;
node(int tx, int ty, int t): x(tx), y(ty), time(t) {
}
};
void bfs() {
firetomap();
queue<node> que;
vis[sx][sy] = 1;
que.push(node(sx, sy, 0));
while (!que.empty()) {
node t = que.front();
que.pop();
//printf("%d %d %d\n", t.x, t.y, t.time);
if (t.x == 1 || t.x == R || t.y == 1 || t.y == C ) {
printf("%d\n", t.time + 1);
return ;
}
for (int i = 0; i <= 3; i++) {
int tx = t.x + go[i][0];
int ty = t.y + go[i][1];
int tm = t.time + 1;
if (tx >= 1 && tx <= R && ty >= 1 && ty <= C && vis[tx][ty] == 0 && s[tx][ty] != '#' && tm < fire[tx][ty] ) {
vis[tx][ty] = 1;
que.push(node(tx, ty, tm));
}
}
}
printf("IMPOSSIBLE\n");
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
pos = 0;
scanf("%d %d", &R, &C);
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
scanf(" %c", *(s + i) + j);
if (s[i][j] == 'J') {
sx = i;
sy = j;
}
if (s[i][j] == 'F') {
Fire[pos].x = i;
Fire[pos++].y = j;
}
}
}
bfs();
memset(vis, 0, sizeof(vis));
memset(Fire, 0, sizeof(Fire));
}
return 0;
}
迷宫问题
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);
typedef pair<int, int> P;
int vis[10][10];
int maze[10][10];
int go[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct node {
int x, y;
string s;
//node(int tx, int ty, const char *ss): x(tx), y(ty), s(ss) {
//}
node(int tx, int ty, string ts): x(tx), y(ty), s(ts) {
}
};
string bfs() {
queue<node> que;
vis[0][0] = 1;
que.push(node(0, 0, "(0, 0)\n"));
while (!que.empty()) {
node t = que.front();
que.pop();
int tx, ty;
//cout << t.s;
if (t.x == 4 && t.y == 4) {
return t.s;
}
for (int i = 0; i <= 3; i++) {
tx = t.x + go[i][0];
ty = t.y + go[i][1];
// printf("%d %d\n", tx, ty);
if (tx >= 0 && tx < 5 && ty >= 0 && ty < 5 && vis[tx][ty] == 0 && maze[tx][ty] == 0) {
vis[tx][ty] = 1;
string s = "(, )\n" ;
char ch1 = '0' + tx, ch2 = '0' + ty;
s.insert(s.begin() + 1, ch1);
s.insert(s.begin() + 4, ch2);
//cout << "S:" << s << endl;
que.push(node(tx, ty, t.s + s));
}
}
}
return "Impossible\n";
}
int main() {
for (int i = 0; i <= 4; i++) {
for (int j = 0; j <= 4; j++) {
scanf("%d", *(maze + i) + j);
}
}
cout << bfs() << endl;
return 0;
}
非常可乐
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);
typedef pair<int, int> P;
struct node {
int x, y, z, step;
node(int tx, int ty, int tz, int c): x(tx), y(ty), z(tz), step(c) {
}
};
int S, N, M;
int vis[105][105][105];
void bfs() {
queue<node> que;
que.push(node(0, 0, S, 0));
int kk = S / 2;
while (!que.empty()) {
node t = que.front();
que.pop();
//printf("%d %d %d %d\n", t.z, t.x, t.y, t.step);
if ((t.z == kk && t.x == kk) || (t.x == kk && t.y == kk) || (t.z == kk && t.y == kk)) {
printf("%d\n", t.step);
return;
}
if (vis[0][t.y][t.x + t.z] == 0) {
vis[0][t.y][t.x + t.z] = 1;
que.push(node(0, t.y, t.x + t.z, t.step + 1));
}
if (vis[t.x][0][t.z + t.y] == 0) {
vis[t.x][0][t.z + t.y] = 1;
que.push(node(t.x, 0, t.y + t.z, t.step + 1));
}
if (t.z + t.x > N && vis[N][t.y][t.z + t.x - N] == 0) {
vis[N][t.y][t.z + t.x - N] = 1;
que.push(node(N, t.y, t.z - (N - t.x), t.step + 1));
} else if (t.z + t.x <= N && vis[t.x + t.z][t.y][0] == 0) {
vis[t.x + t.z][t.y][0] = 1;
que.push(node(t.x + t.z, t.y, 0, t.step + 1));
}
if (t.y + t.z > M && vis[t.x][M][t.y + t.z - M] == 0) {
vis[t.x][M][t.y + t.z - M] = 1;
que.push(node(t.x, M, t.y + t.z - M, t.step + 1));
} else if (t.y + t.z <= M && vis[t.x][t.y + t.z][0] == 0) {
vis[t.x][t.y + t.z][0] = 1;
que.push(node(t.x, t.y + t.z, 0, t.step + 1));
}
if (t.y + t.x > N && vis[N][t.y + t.x - N][t.z] == 0) {
vis[N][t.y + t.x - N][t.z] = 1;
que.push(node(N, t.y + t.x - N, t.z, t.step + 1));
} else if (t.x + t.y <= N && vis[t.x + t.y][0][t.z] == 0) {
vis[t.x + t.y][0][t.z] = 1;
que.push(node(t.x + t.y, 0, t.z, t.step + 1));
}
if (t.y + t.x > M && vis[t.x + t.y - M][M][t.z] == 0) {
vis[t.x + t.y - M][M][t.z] = 1;
que.push(node(t.x + t.y - M, M, t.z, t.step + 1));
} else if (t.y + t.x <= M && vis[t.x + t.y][0][t.z] == 0) {
vis[t.x + t.y][0][t.z] = 1;
que.push(node(t.x + t.y, 0, t.z, t.step + 1));
}
}
printf("NO\n");
}
int main() {
while (scanf("%d %d %d", &S, &N, &M) && (S + N + M)) {
if (S % 2 != 0) {
printf("NO\n");
continue;
}
bfs();
memset(vis, 0, sizeof(vis));
}
return 0;
}