1、UESTC 149 解救小Q
因为有可能传送后需要再回来才能找到 Q,所以每次遇到传送门时只标记当前这个传送门,那么就有可能再传送回来
刚开始以为标记当前传送门以后就回不来了,写的一塌糊涂...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 50 + 10;
char G[maxn][maxn];
int N, M;
int love_x, love_y;
int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool vis[maxn][maxn];
struct Door {
int x1, y1, x2, y2;
bool flag;
};
struct Node {
int x, y;
int Count;
};
Door door[40];
int bfs(void);
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
int T;
scanf("%d", &T);
while (T--) {
memset(vis, false,sizeof(vis));
memset(door, 0, sizeof(door));
scanf("%d%d", &N, &M);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
cin >> G[i][j];
if (G[i][j] == 'L') {
love_x = i; love_y = j;
} else if (islower(G[i][j])) {
int t = G[i][j] - 'a';
if (!door[t].flag) {
door[t].x1 = i; door[t].y1 = j; door[t].flag = true;
} else {
door[t].x2 = i; door[t].y2 = j;
}
}
}
}
printf("%d\n", bfs());
}
return 0;
}
int bfs(void)
{
Node node;
queue<Node> Q;
node.x = love_x; node.y = love_y; node.Count = 0;
Q.push(node);
vis[node.x][node.y] = true;
while (!Q.empty()) {
int x_t = Q.front().x;
int y_t = Q.front().y;
int Count_t = Q.front().Count;
if (G[x_t][y_t] == 'Q') {
return Count_t;
}
Q.pop();
for (int i = 0; i < 4; ++i) {
node.x = x_t + dir[i][0];
node.y = y_t + dir[i][1];
node.Count = Count_t + 1;
if (0 <= node.x && node.x < N && 0 <= node.y && node.y < M && !vis[node.x][node.y] && G[node.x][node.y] != '#') {
vis[node.x][node.y] = true;
if (islower(G[node.x][node.y])) {
int t = G[node.x][node.y] - 'a';
if (door[t].x1 == node.x && door[t].y1 == node.y) {
node.x = door[t].x2; node.y = door[t].y2;
} else {
node.x = door[t].x1; node.y = door[t].y1;
}
}
Q.push(node);
}
}
}
return -1;
}
2、POJ 1426 Find The Multiple
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long LL;
int n;
void bfs(void);
int main()
{
// freopen("in.txt", "r", stdin);
while (scanf("%d", &n) != EOF && n != 0) {
bfs();
}
return 0;
}
void bfs(void)
{
queue<LL> Queue;
Queue.push(1);
while (!Queue.empty()) {
LL t = Queue.front();
if (t%n == 0) {
printf("%I64d\n", t);
return;
}
Queue.pop();
Queue.push(t*10);
Queue.push(t*10+1);
}
}
3、FZU 2150 Fire Game
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> Pair;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 20;
struct Node {
int x, y, step;
};
int n, m, ans, Case = 0, size;
char G[maxn][maxn];
bool vis[maxn][maxn];
int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void bfs(int x1, int y1, int x2, int y2);
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // __AiR_H
int T;
scanf("%d", &T);
int Case = 0;
while (T--) {
vector<Pair> V;
scanf("%d%d", &n, &m);
ans = INF;
for (int i = 0; i < n; ++i) {
scanf("%s", G[i]);
for (int j = 0; j < m; ++j) {
if (G[i][j] == '#') {
V.push_back(make_pair(i, j));
}
}
}
size = V.size();
if (size <= 2) {
printf("Case %d: 0\n", ++Case);
continue;
}
for (int i = 0; i < size-1; ++i) {
for (int j = i+1; j < size; ++j) {
bfs(V[i].first, V[i].second, V[j].first, V[j].second);
}
}
if (ans == INF) {
ans = -1;
}
printf("Case %d: %d\n", ++Case, ans);
}
return 0;
}
void bfs(int x1, int y1, int x2, int y2)
{
memset(vis, false, sizeof(vis));
vis[x1][y1] = vis[x2][y2] = true;
int Max = 0, cnt = 2;
queue<Node> Queue;
Node node;
node.x = x1, node.y = y1, node.step = 0, Queue.push(node);
node.x = x2, node.y = y2, node.step = 0, Queue.push(node);
while (!Queue.empty()) {
int x_t = Queue.front().x, y_t = Queue.front().y, step_t = Queue.front().step;
if (step_t > ans) {
return;
}
if (step_t > Max) {
Max = step_t;
}
Queue.pop();
for (int i = 0; i < 4; ++i) {
node.x = x_t+dir[i][0], node.y = y_t+dir[i][1], node.step = step_t + 1;
if (0 <= node.x && node.x < n && 0 <= node.y && node.y < m && G[node.x][node.y] == '#' && !vis[node.x][node.y]) {
vis[node.x][node.y] = true;
++cnt;
Queue.push(node);
}
}
}
if (cnt == size && Max < ans) {
ans = Max;
}
}
4、HDU 5945 Fxx and game
用优先队列当然更好,不过窝这个暴力 bfs 竟然也过了
当 val_t - 1 <= t 时不能直接 return step_t + 1 。。。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;
const int maxn = 1e6 + 10;
struct Node {
int val, step;
};
int T;
int X, k, t;
bool vis[maxn];
int bfs(void);
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H_
scanf("%d", &T);
while (T--) {
scanf("%d %d %d", &X, &k, &t);
printf("%d\n", bfs());
}
return 0;
}
int bfs(void) {
queue<Node> Q;
Node node;
node.val = X, node.step = 0;
vis[X] = true;
memset(vis, false, sizeof(vis));
Q.push(node);
while (!Q.empty()) {
int val_t = Q.front().val, step_t = Q.front().step;
Q.pop();
if (val_t == 1) {
return step_t;
}
if (val_t - 1 <= t) {
node.val = 1, node.step = step_t + 1;
if (!vis[node.val]) {
Q.push(node), vis[node.val] = true;
}
continue;
}
if (k != 1 && val_t % k == 0) {
node.val = val_t / k, node.step = step_t + 1;
if (!vis[node.val]) {
Q.push(node), vis[node.val] = true;
}
}
node.val = val_t - t, node.step = step_t + 1;
if (!vis[node.val]) {
Q.push(node), vis[node.val] = true;
}
if (k != 1) {
int x = val_t / k;
for (int i = x; val_t - t < i * k && i * k < val_t; --i) {
node.val = i * k, node.step = step_t + 1;
if (!vis[node.val]) {
Q.push(node), vis[node.val] = true;
}
}
}
}
return 0;
}
5、UVa 12558 Egyptian Fractions (HARD version)
注意:要用 ll
第一次做时限15s的题,T了好几次。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> Pair;
const int INF = 0x7fffffff;
struct Node {
ll x, y;
int cnt;
ll num[20];
};
int a, b, k, ans_cnt = 0;
int key[10];
ll ans[20];
bool bfs(int depth);
bool can_t(ll x);
bool cmp(ll* x, ll *y, ll len);
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // __AiR_H
int T, Case = 0;
scanf("%d", &T);
while (T--) {
scanf("%d %d %d", &a, &b, &k);
REP(i, k) { scanf("%d", &key[i]); }
int d = 1;
ans_cnt = 0;
while (1) {
if (bfs(d)) { break; }
++d;
}
printf("Case %d: %d/%d=", ++Case, a, b);
REP(i, ans_cnt - 1) { printf("1/%lld+", ans[i]); }
printf("1/%lld\n", ans[ans_cnt - 1]);
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}
bool cmp(ll* x, ll *y, int len) {
for (int i = len - 1; i >= 0; --i) {
if (x[i] < y[i]) { return true; }
if (x[i] > y[i]) { return false; }
}
return false;
}
bool bfs(int depth) {
queue<Node> q;
Node node;
bool flag = false;
ll t_1, x_t, y_t, gcd;
t_1 = depth * b;
for (ll i = b / a; a * i <= t_1; ++i) {
if (a * i < b) { continue; }
if (can_t(i)) { continue; }
node.x = a * i - b; node.y = b * i;
gcd = __gcd(node.x, node.y); node.x /= gcd; node.y /= gcd;
node.cnt = 1; node.num[0] = i; q.push(node);
}
while (!q.empty()) {
Node t = q.front(); q.pop();
if (flag && t.num[t.cnt - 1] > ans[ans_cnt - 1]) { continue; }
if (t.x == 0) {
if (ans_cnt < t.cnt || cmp(t.num, ans, ans_cnt)) {
ans_cnt = t.cnt;
REP(i, ans_cnt) { ans[i] = t.num[i]; }
}
flag = true; continue;
}
if (t.cnt == depth) { continue; }
t_1 = (depth - t.cnt) * t.y;
for (ll i = max(t.num[t.cnt - 1] + 1, t.y / t.x); t.x * i <= t_1; ++i) {
if (flag && i > ans[ans_cnt - 1]) { break; }
if (t.x * i < t.y) { continue; }
if (can_t(i)) { continue; }
x_t = t.x * i - t.y; y_t = i * t.y;
gcd = __gcd(x_t, y_t); x_t /= gcd; y_t /= gcd;
node.x = x_t; node.y = y_t;
REP(i, t.cnt) { node.num[i] = t.num[i]; }
node.cnt = t.cnt + 1; node.num[t.cnt] = i;
q.push(node);
}
}
return flag;
}
bool can_t(ll x) {
REP(i, k) { if (x == (ll)key[i]) { return true; } }
return false;
}