Kuangbin带你飞专题一 简单搜索
记得E和M比较难看了题解,其他还好
A:POJ 2321
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 10;
const int MAXM = 1 << MAXN;
long long dp[MAXN][MAXM];
char g[MAXN][MAXN];
int n, num;
long long dfs(int row, int state, int cnt)
{
// printf("row = %d, state = %d, cnt = %d\n", row, state, cnt);
// system("pause");
if(dp[row][state] > 0) return dp[row][state];
long long temp = 0;
for(int i = 0 ; i < n ; i++) {
if(g[row][i] == '#' && (((1 << i) & state) == 0)) {
if(cnt == 0) temp++;
else{
for(int j = row + 1 ; j + cnt - 1 < n ; j++) {
temp += dfs(j, (state | (1 << i)) , cnt - 1);
}
}
}
}
return dp[row][state] = temp;
}
int main()
{
while(scanf("%d%d", &n, &num) != EOF && n != -1) {
for(int i = 0 ; i < n ; i++) scanf("%s", g[i]);
// printf("g[0] = %s\n", g[0]);
long long ans = 0;
memset(dp, 0, sizeof(dp));
///init dp[0]
// for(int i = 0 ; i < (1 << n) ; i++) {
// for(int j = 0 ; j < n ; j++) {
// if(((1 << j) & i) && g[0][j] == '#') dp[0][i]++;
// }
// }
for(int i = 0 ; i + num - 1 < n ; i++) ans += dfs(i, 0, num - 1);
cout << ans << endl;
}
return 0;
}
B:POJ 2251
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 30 + 5;
char g[MAXN][MAXN][MAXN];
int dis[MAXN][MAXN][MAXN];
int dx[] = {-1, 1, 0, 0, 0, 0};
int dy[] = {0, 0, -1, 1, 0, 0};
int dz[] = {0, 0, 0, 0, -1, 1};
int n, m, l;
bool valid(int x, int y, int z){if(x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < l) return true; else return false;}
struct Node
{
int x, y, z;
};
queue<Node>que;
int main()
{
while(scanf("%d%d%d", &n, &m, &l) != EOF && n + m + l) {
memset(dis, -1, sizeof dis);
for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j++) scanf("%s", g[i][j]);
int sx, sy, sz;
int ex, ey, ez;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
for(int k = 0 ; k < l ; k++) {
if(g[i][j][k] == 'S') sx = i, sy = j, sz = k;
if(g[i][j][k] == 'E') ex = i, ey = j, ez = k;
}
}
}
dis[sx][sy][sz] = 0;
Node temp = Node{sx, sy, sz};
while(!que.empty()) que.pop();
que.push(temp);
while(!que.empty()) {
Node temp = que.front(); que.pop();
for(int i = 0 ; i < 6 ; i++) {
int tx = temp.x + dx[i];
int ty = temp.y + dy[i];
int tz = temp.z + dz[i];
if(valid(tx,ty,tz) && dis[tx][ty][tz] == -1 && g[tx][ty][tz] != '#') {
dis[tx][ty][tz] = dis[temp.x][temp.y][temp.z] + 1;
que.push(Node{tx,ty,tz});
}
}
}
if(dis[ex][ey][ez] == -1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", dis[ex][ey][ez]);
}
return 0;
}
C:POJ 3278
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 100000 + 5;
int dis[MAXN * 2];
int main()
{
int n, k;
while(scanf("%d%d", &n, &k) != EOF) {
queue<int> que;
memset(dis, -1, sizeof dis);
dis[n] = 0;
que.push(n);
while(!que.empty()) {
int u = que.front(); que.pop();
if(u >= 1 && dis[u - 1] == -1) dis[u - 1] = dis[u] + 1, que.push(u - 1);
if(u <= k && dis[u + 1] == -1) dis[u + 1] = dis[u] + 1, que.push(u + 1);
if(u * 2 <= 2 * k && dis[u * 2] == -1) dis[u * 2] = dis[u] + 1, que.push(u * 2);
}
printf("%d\n", dis[k]);
}
return 0;
}
D:POJ 3279
using namespace std;
const int MAXN = 15 + 2;
const int MAXM = 1 << MAXN;
int n, m;
int g[MAXN][MAXN];
int out[MAXN][MAXN];
bool validPos(int x, int y){if(x >= 0 && x < n && y >= 0 && y < m) return true; else return false;}
int dx[] = {0, -1, 0, 0};
int dy[] = {-1, 0, 1, 0};
int ans[MAXN][MAXN];
bool valid(int state)
{
for(int i = 0 ; i < m ; i++) if((1 << i) & state) out[0][m - i - 1] = 1;
for(int i = 1 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
int temp = 0;
for(int k = 0 ; k < 4 ; k++) {
if(validPos(i - 1 + dx[k], j + dy[k])) {
temp += out[i - 1 + dx[k]][j + dy[k]];
}
}
temp %= 2;
if(temp == g[i - 1][j]) out[i][j] = 0;
else out[i][j] = 1;
}
}
for(int j = 0 ; j < m ; j++) {
int temp = 0;
for(int k = 0 ; k < 4 ; k++) if(validPos(n - 1 + dx[k], j + dy[k])) temp += out[n - 1 + dx[k]][j + dy[k]];
temp %= 2;
if(temp != g[n - 1][j]) return false;
}
return true;
}
//int ans[MAXN][MAXN];
//bool valid2(int re, int cmp)
//{
// for(int i = 0 ; i < m ; i++) {
// bool t1 = ((1 << i) & re);
// bool t2 = ((1 << i) & cmp);
// if(t1 == 0 && t2 == 1) return false;
// else if(t1 == 1 && t2 == 0) return true;
// }
// return false;
//}
int main()
{
while(scanf(“%d%d”, &n, &m) != EOF) {
for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j++) scanf(“%d”, &g[i][j]);
int ok = 0;
int cnt = 100000007;
for(int i = 0 ; i < (1 << m) ; i++)
if(valid(i)) {
ok = 1;
int num = 0;
for(int j = 0 ; j < n ; j++) for(int k = 0 ; k < m ; k++) if(out[j][k]) num++;
if(num < cnt) {
cnt = num;
for(int j = 0 ; j < n ; j++) for(int k = 0 ; k < m ; k++) ans[j][k] = out[j][k];
}
}
if(ok == 0) puts(“IMPOSSIBLE”);
else {
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
printf(“%d”, ans[i][j]);
if(j == m - 1) puts(“”);
else printf(” “);
}
}
}
}
return 0;
}
E:POJ 1426
using namespace std;
typedef pair
F:POJ 3216
using namespace std;
const int MAXN = 10000 + 5;
queueque;
bool prime[MAXN];
int dis[MAXN];
void init()
{
int vis[MAXN];
memset(vis, 0, sizeof vis);
memset(prime, false, sizeof prime);
for(int i = 2 ; i < MAXN ; i++) {
if(vis[i] == 0) {
prime[i] = true;
for(int j = i ; j < MAXN ; j += i) vis[j] = 1;
}
}
}
int replace(int u, int v, int mark)
{
if(mark == 0) return u / 10 * 10 + v;
else if(mark == 1) {
return u / 100 * 100 + u % 10 + v * 10;
}
else if(mark == 2) {
return u / 1000 * 1000 + u % 100 + v * 100;
}
else return u % 1000 + v * 1000;
}
int main()
{
init();
int T;
scanf(“%d”, &T);
while(T–) {
int s, t;
scanf(“%d%d”, &s, &t);
memset(dis, -1, sizeof dis);
dis[s] = 0;
while(!que.empty()) que.pop();
que.push(s);
while(!que.empty()) {
int u = que.front(); que.pop();
for(int i = 0 ; i <= 9 ; i++) {
for(int j = 0 ; j < 4 ; j++) {
if(i == 0 && j == 3) continue;
int temp = replace(u, i, j);
if(dis[temp] == -1 && prime[temp]) {
que.push(temp);
dis[temp] = dis[u] + 1;
}
}
}
}
if(dis[t] == -1) puts(“impossible”);
else printf(“%d\n”, dis[t]);
}
return 0;
}
G: POJ 3087
using namespace std;
const int MAXN = 200 + 5;
string s1, s2;
string dest;
int n;
string syn(string s1, string s2)
{
string ans = “”;
for(int i = 0 ; i < n ; i++) {
ans += s2[i];
ans += s1[i];
}
return ans;
}
int main()
{
int T;
scanf(“%d”, &T);
for(int cas = 1 ; cas <= T ; cas++) {
scanf(“%d”, &n);
cin >> s1 >> s2 >> dest;
int ans = -1;
for(int i = 1 ; i <= 2 * n ; i++) {
string temp = syn(s1, s2);
// if(i <= 2) cout <<”i = ” << i << ” temp = ” << temp << endl;
if(temp == dest) {
ans = i;
break;
}
s1 = temp.substr(0, n);
s2 = temp.substr(n, 2 * n);
}
printf(“%d %d\n”, cas, ans);
}
return 0;
}
H: POJ 3414
using namespace std;
typedef pair
const int MAXN = 100 + 5;
int dis[MAXN][MAXN];
pii path[MAXN][MAXN];
int op[MAXN][MAXN];
int n, m, dest;
queueque;
int str[MAXN * MAXN];
void print(int mark)
{
if(mark == 0) puts(“FILL(1)”);
if(mark == 1) puts(“FILL(2)”);
if(mark == 2) puts(“DROP(1)”);
if(mark == 3) puts(“DROP(2)”);
if(mark == 4) puts(“POUR(1,2)”);
if(mark == 5) puts(“POUR(2,1)”);
}
int main()
{
while(scanf(“%d%d%d”, &n, &m, &dest) != EOF) {
while(!que.empty()) que.pop();
for(int i = 0 ; i <= n ; i++) for(int j = 0 ; j <= m ; j++) path[i][j] = mp(i, j);
que.push(mp(n, m));
memset(dis, -1, sizeof dis);
dis[n][m] = 0;
while(!que.empty()) {
pii u = que.front(); que.pop();
pii v;
if(u.fi + u.se <= m) v.fi = 0, v.se = u.fi + u.se;
else v.fi = u.fi + u.se - m, v.se = m;
if(dis[v.fi][v.se] == -1) dis[v.fi][v.se] = dis[u.fi][u.se] + 1, que.push(v), path[v.fi][v.se] = u, op[v.fi][v.se] = 4;
if(u.fi + u.se <= n) v.se = 0, v.fi = u.fi + u.se;
else v.se = u.fi + u.se - n, v.fi = n;
if(dis[v.fi][v.se] == -1) dis[v.fi][v.se] = dis[u.fi][u.se] + 1, que.push(v), path[v.fi][v.se] = u, op[v.fi][v.se] = 5;
if(dis[n][u.se] == -1) dis[n][u.se] = dis[u.fi][u.se] + 1, que.push(mp(n, u.se)), path[n][u.se] = u, op[n][u.se] = 0;
if(dis[u.fi][m] == -1) dis[u.fi][m] = dis[u.fi][u.se] + 1, que.push(mp(u.fi, m)), path[u.fi][m] = u, op[u.fi][m] = 1;
if(dis[0][u.se] == -1) dis[0][u.se] = dis[u.fi][u.se] + 1, que.push(mp(0, u.se)), path[0][u.se] = u, op[0][u.se] = 2;
if(dis[u.fi][0] == -1) dis[u.fi][0] = dis[u.fi][u.se] + 1, que.push(mp(u.fi, 0)), path[u.fi][0] = u, op[u.fi][0] = 3;
}
int ans = MAXN * MAXN + 4;
pii re;
for(int i = 0 ; i <= m ; i++) if(dis[dest][i] != -1 && ans > dis[dest][i]) ans = dis[dest][i], re = mp(dest, i);
for(int i = 0 ; i <= n ; i++) if(dis[i][dest] != -1 && ans > dis[i][dest]) ans = dis[i][dest], re = mp(i, dest);
if(ans == MAXN * MAXN + 4) puts("impossible");
else {
int cnt = 0;
while(path[re.fi][re.se] != re) {
str[cnt++] = op[re.fi][re.se];
re = path[re.fi][re.se];
}
printf("%d\n", ans);
for(int i = cnt - 1 ; i >= 0 ; i--) {
print(str[i]);
}
}
}
return 0;
}
I:FZU 2150
using namespace std;
typedef pair
J UVA 11624
using namespace std;
typedef pair
queueque;
const int MAXN = 1000 + 5;
char g[MAXN][MAXN];
int cost[MAXN][MAXN];
int dis[MAXN][MAXN];
int n, m;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
bool valid(int x,int y){if(x >= 0 && x < n && y >= 0 && y < m) return true; else return false;}
int main()
{
int T;
scanf(“%d”, &T);
while(T–) {
scanf(“%d%d”, &n, &m);
for(int i = 0 ; i < n ; i++) scanf(“%s”, g[i]);
int x1, y1;
while(!que.empty()) que.pop();
memset(cost, -1, sizeof cost);
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(g[i][j] == ‘J’) x1 = i, y1 = j;
if(g[i][j] == ‘F’) {
que.push(mp(i, j));
cost[i][j] = 0;
}
}
}
while(!que.empty()) {
pii temp = que.front(); que.pop();
int x = temp.fi, y = temp.se;
for(int i = 0 ; i < 4 ; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if(valid(tx, ty) && g[tx][ty] != '#' && cost[tx][ty] == -1) {
cost[tx][ty] = cost[x][y] + 1;
que.push(mp(tx, ty));
}
}
}
while(!que.empty()) que.pop();
memset(dis, -1, sizeof dis);
dis[x1][y1] = 0;
que.push(mp(x1, y1));
int ans = -1;
while(!que.empty()) {
pii temp = que.front(); que.pop();
int x = temp.fi, y = temp.se;
for(int i = 0 ; i < 4 ; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if(!valid(tx, ty)) {
ans = dis[x][y] + 1;
break;
}
else if(g[tx][ty] != '#' && dis[tx][ty] == -1 && dis[x][y] + 1 < cost[tx][ty]) {
dis[tx][ty] = dis[x][y] + 1;
que.push(mp(tx, ty));
}
}
if(ans != -1) break;
}
if(ans == -1) puts("IMPOSSIBLE");
else printf("%d\n", ans);
}
return 0;
}
K POJ 3984
using namespace std;
typedef pair
L HDU 1241
M HDU 1459
N HDU 2612
const int MAXN = 200 + 5;
int d1[MAXN][MAXN], d2[MAXN][MAXN];
char g[MAXN][MAXN];
int n, m;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
bool valid(int x, int y){if(x >= 0 && x < n && y >= 0 && y < m) return true; else return false;}
void bfs(int u, int v, int d[MAXN][MAXN])
{
queue que;
que.push(mp(u, v));
memset(d, -1, sizeof d);
d[u][v] = 0;
while(!que.empty()) {
int x = que.front().fi;
int y = que.front().se;
que.pop();
for(int i = 0 ; i < 4 ; i++) {
int tx = dx[i] + x;
int ty = dy[i] + y;
if(valid(tx, ty) && d[tx][ty] == -1 && g[tx][ty] != ‘#’) {
d[tx][ty] = d[x][y] + 1;
que.push(mp(tx, ty));
}
}
}
}
int main()
{
while(scanf(“%d%d”, &n, &m) != EOF) {
for(int i = 0 ; i < n ; i++) scanf(“%s”, g[i]);
int x1, y1, x2, y2;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(g[i][j] == ‘Y’) x1 = i, y1 = j;
if(g[i][j] == ‘M’) x2 = i, y2 = j;
}
}
queue que;
que.push(mp(x1, y1));
memset(d1, -1, sizeof d1);
d1[x1][y1] = 0;
while(!que.empty()) {
int x = que.front().fi;
int y = que.front().se;
que.pop();
for(int i = 0 ; i < 4 ; i++) {
int tx = dx[i] + x;
int ty = dy[i] + y;
if(valid(tx, ty) && d1[tx][ty] == -1 && g[tx][ty] != ‘#’) {
d1[tx][ty] = d1[x][y] + 1;
que.push(mp(tx, ty));
}
}
}
que.push(mp(x2, y2));
memset(d2, -1, sizeof d2);
d2[x2][y2] = 0;
while(!que.empty()) {
int x = que.front().fi;
int y = que.front().se;
que.pop();
for(int i = 0 ; i < 4 ; i++) {
int tx = dx[i] + x;
int ty = dy[i] + y;
if(valid(tx, ty) && d2[tx][ty] == -1 && g[tx][ty] != ‘#’) {
d2[tx][ty] = d2[x][y] + 1;
que.push(mp(tx, ty));
}
}
}
// for(int i = 0 ; i < n ; i++) {
// for(int j = 0 ; j < m ; j++) printf(“%d “, d1[i][j]);
// printf(“\n”);
// }
int ans = 1000000007;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(g[i][j] == ‘@’ && d1[i][j] != -1 && d2[i][j] != -1)
ans = min(ans, d1[i][j] + d2[i][j]);
}
}
printf(“%d\n”, ans * 11);
}
return 0;
}
“`