您的位置:首页 > 其它

搜索专题小结(一)hdu 4634 1428 4277 1044 1043

2014-12-25 21:39 459 查看
hdu 4634 Swipe Bo

题意:让Bo向四个方向移动,如果碰到了墙Bo就会停下来,如果遇到转弯标识就要转向,直到搜集完全部钥匙后到达出口,求能否完成任务并输出最少步数。

思路:这个题在标记状态的时候需要加一维记录方向,并将方向的判断放在最前面,题目不是很难,就是细节特别多。

AC代码:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int mov[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}, nkey, xx, yy;
int m, n;
bool vis[205][205][4][130], vis2[205][205][130];
char map[205][205], dre[] = "DURL";
struct Node {
int state, steps, x, y;
Node(int a, int b){
x = a, y = b;
state = steps = 0;
}
};
queue<Node>q;
int bfs() {
memset(vis, 0, sizeof vis);
memset(vis2, 0, sizeof vis2);
while(!q.empty()) q.pop();
Node node(xx, yy);
q.push(node);
while(!q.empty()) {
node = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
Node t = node;
int d = i;
int a = mov[i][0], b = mov[i][1];
if(t.x + a < 1 || t.x + a > m || t.y + b < 1 || t.y + b > n) continue;
if(map[t.x + a][t.y + b] == '#') continue;
t.steps++;
while(true) {
for(int j = 0; dre[j]; j++)//change direction
if(map[t.x][t.y] == dre[j])
a = mov[j][0], b = mov[j][1], d = j;
if(t.x < 1 || t.x > m || t.y < 1 || t.y > n) break;//out
if(vis[t.x][t.y][d][t.state]) break;
vis[t.x][t.y][d][t.state] = true;
// printf("====%d %d %d %d\n", t.x, t.y, t.steps, t.state);
t.x += a, t.y += b;
if(map[t.x][t.y] == 'E') {//finish
if(t.state == (1 << nkey) - 1) return t.steps;
}
if(map[t.x][t.y] >= '0' && map[t.x][t.y] <= '9')//key
t.state |= (1 << (map[t.x][t.y] - '0'));
// vis[t.x][t.y][d][t.state] = true;
if(map[t.x][t.y] == '#') {
t.x -= a, t.y -= b;
if(vis2[t.x][t.y][t.state]) break;
vis2[t.x][t.y][t.state] = true;
q.push(t);
break;
}
}
}
}
return -1;
}
main() {
//freopen("in.in", "r", stdin);
while(~scanf("%d %d", &m, &n)) {
getchar();
memset(map, 0, sizeof map);
nkey = 0;
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= n; j++) {
map[i][j] = getchar();
if(map[i][j] == 'K')
map[i][j] = '0' + nkey++;
if(map[i][j] == 'S')
xx = i, yy = j;
}
getchar();
}
printf("%d\n", bfs());
}
}hdu 1428 漫步校园
题意:题目虽然是中文的但是我看不懂。。题意就是问从(1,1)到(n,n)有几条路可以走,前提是如果A点到(n,n)的最短距离小于B点到(n,n)的最短距离才会从A走到B。

思路:两遍bfs,第一遍记录各点到(n,n)的距离,第二遍计算从(1,1)到各点的路径数。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int M = 55;
struct Node {
int x, y, steps;
bool operator < (const Node &i) const {
return steps < i.steps;
}
};
int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}, n, map[M][M];
int vis[M][M];
bool vi[M][M];
long long dp[M][M];
priority_queue<Node> pq;
long long bfs() {
while(!pq.empty()) pq.pop();
Node node;
node.x = node.y = n;
node.steps = 0;
pq.push(node);
memset(vis, -1, sizeof vis);
while(!pq.empty()) {
node = pq.top();
pq.pop();
if(vis[node.x][node.y] != -1 && vis[node.x][node.y] <= -node.steps) continue;
vis[node.x][node.y] = -node.steps;
for(int i = 0; i < 4; i++) {
int x = node.x + d[i][0], y = node.y + d[i][1];
if(x > n || y > n || x < 1 || y < 1) continue;
Node t = node;
t.x = x, t.y = y, t.steps -= map[x][y];
pq.push(t);
}
}
memset(vi, 0, sizeof vi);
while(!pq.empty()) pq.pop();
node.x = node.y = 1, node.steps = vis[1][1];
pq.push(node);
memset(dp, 0, sizeof dp);
dp[1][1] = 1;
while(!pq.empty()) {
node = pq.top();
pq.pop();
if(vi[node.x][node.y]) continue;
vi[node.x][node.y] = true;
for(int i = 0; i < 4; i++) {
int x = node.x + d[i][0], y = node.y + d[i][1];
if(x > n || y > n || x < 1 || y < 1) continue;
if(vis[x][y] >= vis[node.x][node.y]) continue;

dp[x][y] += dp[node.x][node.y];
Node t = node;
t.x = x, t.y = y, t.steps = vis[x][y];
pq.push(t);
}
}
return dp

;
}
main() {
while(~scanf("%d", &n)) {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &map[i][j]);
printf("%I64d\n", bfs());
}
}hdu 4277 USACO ORZ
题意:给几根木棍,随意拼接,能组成几种不同的三角形。

思路:这个题很卡时间。。思路很简单就是dfs+set判重,一个小小的简直tle了那么久。。真是orz

AC代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <set>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
set<P> st;
int len[20], n, ans, sum, a, b;
void dfs(int now) {
if(now == n) {
if(a == 0 || b == 0 || sum == a + b) return ;
int side[3];
side[0] = a, side[1] = b, side[2] = sum - a - b;
sort(side, side + 3);
if(side[0] <= side[2] - side[1] || side[0] + side[1] <= side[2]) return;
st.insert(make_pair(side[0], side[1]));
return ;
}
dfs(now + 1);
a += len[now];
dfs(now + 1);
a -= len[now];
b += len[now];
dfs(now + 1);
b -= len[now];
}
main() {
int t;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
sum = 0;
for(int i = 0; i < n; i++)
scanf("%d", &len[i]), sum += len[i];
st.clear();
a = len[0], b = 0;
dfs(1);//一定要从1开始 否则会tle
printf("%d\n", st.size());
}
}
hdu 1044 Collect More Jewels
题意:在规定时间内收集尽量价值总和高的东西然后逃出去。

思路:对于起始点、出口、宝物所在地进行bfs,得到互相到达的最短距离,然后dfs枚举取哪几个物品或者不取,输出最大值。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int w, h, m, l, ts[15], mx, my, ex, ey, sum, txy[15][2];
int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}, ans;
char map[55][55];

struct Node {
int x, y, steps;
Node(int a = 0, int b = 0) {
x = a, y = b;
steps = 0;
}
};
bool vis[55][55];
int tre[15][15];
queue<Node>q;
void bfs(int x, int y, int a) {
memset(vis, 0, sizeof vis);
while(!q.empty()) q.pop();
Node node(x, y);
q.push(node);
while(!q.empty()) {
node = q.front();
q.pop();
if(vis[node.x][node.y]) continue;
vis[node.x][node.y] = true;
if(map[node.x][node.y] >= 'A' && map[node.x][node.y] <= 'J')
tre[a][map[node.x][node.y] - 'A' + 1] = tre[map[node.x][node.y] - 'A' + 1][a] = node.steps;
if(map[node.x][node.y] == '@')
tre[a][0] = tre[0][a] = node.steps;
if(map[node.x][node.y] == '<')
tre[a][m + 1] = tre[m + 1][a] = node.steps;
for(int i = 0; i < 4; i++) {
Node tmp = node;
int tx = tmp.x + d[i][0], ty = tmp.y + d[i][1];
if(tx < 1 || ty < 1 || tx > h || ty > w || map[tx][ty] == '*') continue;
tmp.steps++;
tmp.x = tx, tmp.y = ty;
q.push(tmp);
}
}
}
void dfs(int state, int dis, int now, int ret) {
if(ans == sum) return ;
if(dis + tre[now][m + 1] > l) return ;
ans = max(ans, ret);
for(int i = 1; i <= m; i++) {
if((state & (1 << i))) continue;
dfs(state | (1 << i), dis + tre[now][i], i, ret + ts[i]);
}
}
main() {
int t;
// freopen("in.in", "r", stdin);
scanf("%d", &t); getchar();
for(int T = 1; T <= t; T++) {
memset(tre, 0, sizeof tre);
memset(txy, 0, sizeof txy);
for(int i = 0; i <= m + 1; i++)
for(int j = 0; j <= m + 1; j++)
tre[i][j] = 0x7fffff;
getchar();
sum = 0;
scanf("%d %d %d %d", &w, &h, &l, &m);
for(int i = 1; i <= m; i++) scanf("%d", &ts[i]), sum += ts[i];
getchar();
for(int i = 1; i <= h; i++) {
for(int j = 1; j <= w; j++) {
map[i][j] = getchar();
if(map[i][j] == '@')
mx = i, my = j;
if(map[i][j] == '<')
ex = i, ey = j;
if(map[i][j] >= 'A' && map[i][j] <= 'J')
txy[map[i][j] - 'A'][0] = i, txy[map[i][j] - 'A'][1] = j;
}
getchar();
}
bfs(mx, my, 0);
bfs(ex, ey, m + 1);
for(int i = 0; i < m; i++)
bfs(txy[i][0], txy[i][1], i + 1);
ans = -1;
dfs(1, 0, 0, 0);
printf("Case %d:\n", T);
if(ans == -1) puts("Impossible");
else printf("The best score is %d.\n", ans);
if(T != t) putchar('\n');
}
}
hdu 1043 Eight
题意:8数码问题,移动一个空格来交换数字位置问能否让数字有序,并输出任意一种排序方案。

思路:交换数字位置可以改变增加或减少偶数个逆序对,所以如果逆序对是奇数,那一定无解,所以先判断逆序对,因为状态数也不多,只有9! / 2种(有一半无解),所以可以打表打出来。

另外这题也可以用A*或双向bfs做,但是我两个都敲了两个都tle看来还是太嫩了。。

打表AC:

#include <cstdio>
#include <map>
#include <queue>
#include <iostream>
using namespace std;
int d[4][2] = {0, -1, 0, 1, -1, 0, 1, 0};
int sx, sy;
long long ste;
map<int, string> mp;
string str, dir = "lrud", ans1, ans2;
struct Node {
int x, y;
long long state;
string s;
Node() {
x = y = state = 0;
s.clear();
}
};
long long mov(long long a, int x1, int y1, int x2, int y2) {
int nums[3][3] = {0}, cnt = 8, ret = 0;
while(a) {
nums[cnt / 3][cnt % 3] = a % 10;
a /= 10;
cnt--;
}
nums[x1][y1] = nums[x1][y1] ^ nums[x2][y2];
nums[x2][y2] = nums[x1][y1] ^ nums[x2][y2];
nums[x1][y1] = nums[x1][y1] ^ nums[x2][y2];
for(int i = 0; i < 9; i++)
ret = ret * 10 + nums[i / 3][i % 3];
return ret;
}
queue<Node> q;
void bfs() {
while(!q.empty()) q.pop();
Node node;
node.x = node.y = 2, node.state = 123456780;//mp
q.push(node);
while(!q.empty()) {
node = q.front();
q.pop();
if(mp.find(node.state) != mp.end()) continue;
mp[node.state] = node.s;
for(int i = 0; i < 4; i++) {
Node tmp = node;
int x = node.x + d[i][0], y = node.y + d[i][1];
if(x < 0 || y < 0 || x > 2 || y > 2) continue;
long long a = mov(tmp.state, tmp.x, tmp.y, x, y);
tmp.s += dir[i];
tmp.x = x, tmp.y = y;
tmp.state = a;
q.push(tmp);
}
}
}
string rvs(string s) {
for(int i = 0, j = s.size() - 1; i < j; i++, j--) {
char t = s[i];
s[i] = s[j];
s[j] = t;
}
for(int i = 0; i < s.size(); i++)
if(s[i] == 'l') s[i] = 'r';
else if(s[i] == 'r') s[i] = 'l';
else if(s[i] == 'u') s[i] = 'd';
else if(s[i] == 'd') s[i] = 'u';
return s;
}
main() {
// freopen("in.in", "r", stdin);
bfs();
while(getline(cin, str)) {
ste = 0;
int cnt = 0;
for(int i = 0; i < str.size(); i++) {
if(str[i] >= '1' && str[i] <= '9')
ste = ste * 10 + str[i] - '0', cnt++;
else if(str[i] == 'x')
sx = cnt / 3, sy = cnt % 3, ste = ste * 10, cnt++;
}
cnt = 0;
for(int i = str.size(); i >= 0; i--) {
if(str[i] != ' ' && str[i] != 'x')
for(int j = i - 1; j >= 0; j--) {
if(str[j] != ' ' && str[j] != 'x' && str[j] < str[i])
cnt++;
}
}
if(cnt % 2) {
cout << "unsolvable" << endl;
}
else {
cout << rvs(mp[ste]) << endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: