【HDOJ】1813 Escape from Tetris

bfs预处理一点到边界的最小距离,IDA*求出可行方案。注意按字典序初始化dir数组。并且存在中间点全为1,边界含0的可能性(wa了很多次)。此时不输出任何命令。

  1 /* 1813 */
  2 #include <iostream>
  3 #include <queue>
  4 #include <algorithm>
  5 #include <cstdio>
  6 #include <cstring>
  7 #include <cstdlib>
  8 using namespace std;
  9
 10 #define MAXN 80
 11 #define INF  0xffffff
 12
 13 int n, m;
 14 char ops[4][6] = {
 15     "east",
 16     "north",
 17     "south",
 18     "west"
 19 };
 20 int op[1005];
 21 int x[MAXN], y[MAXN];
 22 int dis[9][9];
 23 int visit[9][9];
 24 char map[9][9];
 25 int dir[4][2] = {
 26     0,1,-1,0,1,0,0,-1
 27 };
 28
 29 inline bool isEnd(int x, int y) {
 30     return x==0 || x==n-1 || y==0 || y==n-1;
 31 }
 32
 33 inline bool check(int x, int y) {
 34     return x<0 || x>=n || y<0 || y>=n;
 35 }
 36
 37 int bfs(int x, int y) {
 38     int xx, yy;
 39     int i, j, k;
 40     queue<int> Q;
 41
 42     k = 10*x + y;
 43     Q.push(k);
 44     memset(visit, -1, sizeof(visit));
 45     visit[x][y] = 0;
 46
 47     while (!Q.empty()) {
 48         k = Q.front();
 49         Q.pop();
 50         x = k/10;
 51         y = k%10;
 52         for (i=0; i<4; ++i) {
 53             xx = x + dir[i][0];
 54             yy = y + dir[i][1];
 55             if (map[xx][yy]==‘1‘ || visit[xx][yy]>=0)
 56                 continue;
 57             visit[xx][yy] = visit[x][y]+1;
 58             if (isEnd(xx, yy))
 59                 return visit[xx][yy];
 60             k = 10*xx + yy;
 61             Q.push(k);
 62         }
 63     }
 64     return 0;
 65 }
 66
 67 bool dfs(int d, int *px, int *py) {
 68     int mmax = -1;
 69     int x[MAXN], y[MAXN];
 70     int i, j, k;
 71
 72     for (i=0; i<m; ++i)
 73         mmax = max(mmax, dis[px[i]][py[i]]);
 74     if (mmax > d)
 75         return false;
 76     if (d == 0)
 77         return true;
 78
 79     for (i=0; i<4; ++i) {
 80         op[d] = i;
 81         for (j=0; j<m; ++j) {
 82             x[j] = px[j] + dir[i][0];
 83             y[j] = py[j] + dir[i][1];
 84             if (isEnd(px[j], py[j]) || map[x[j]][y[j]]==‘1‘) {
 85                 x[j] = px[j];
 86                 y[j] = py[j];
 87             }
 88         }
 89         if (dfs(d-1, x, y))
 90             return true;
 91     }
 92     return false;
 93 }
 94
 95 int main() {
 96     int t = 0;
 97     int i, j, k;
 98
 99     #ifndef ONLINE_JUDGE
100         freopen("data.in", "r", stdin);
101         freopen("data.out", "w", stdout);
102     #endif
103
104     while (scanf("%d",&n) != EOF) {
105         m = 0;
106         for (i=0; i<n; ++i)
107             scanf("%s", map[i]);
108         for (i=0; i<n; ++i) {
109             for (j=0; j<n; ++j) {
110                 if (map[i][j] == ‘1‘) {
111                     dis[i][j] = INF;
112                 } else {
113                     if (isEnd(i, j)) {
114                         dis[i][j] = 0;
115                     } else {
116                         dis[i][j] = bfs(i, j);
117                         x[m] = i;
118                         y[m] = j;
119                         ++m;
120                     }
121                 }
122             }
123         }
124         if (t++)
125             printf("\n");
126         if (m) {
127             for (i=1; ; ++i) {
128                 if (dfs(i, x, y))
129                     break;
130             }
131             for (j=i; j>0; --j)
132                 printf("%s\n", ops[op[j]]);
133         }
134     }
135
136     return 0;
137 }
时间: 2024-10-06 00:28:15

【HDOJ】1813 Escape from Tetris的相关文章

【HDOJ】1811 Rank of Tetris

并查集+拓扑排序.使用并查集解决a = b的情况. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 10005 8 9 typedef struct ArcNode { 10 int adjvex; 11 ArcNode *next; 12 } ArcNode; 1

【HDOJ】2364 Escape

bfs.题目做的不细心,好多小错误.尤其注意起始点就是边界的情况.wa了八次. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 85 8 9 typedef struct node_st { 10 int x, y; 11 int d, s; 12 node_st(

【HDOJ】1760 A New Tetris Game

博弈,主要是求SG值.终于做出点儿感觉. 1 /* 1760 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 55 7 8 char map[MAXN][MAXN]; 9 int n, m; 10 11 int cal_SG() { 12 int i, j, k, r, p; 13 14 for (i=0; i<n-1; ++i) { 15 for

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t