题目大意:
给一个棋盘,分布着蛇和梯子,投骰子确定走的步数,问最少投几次骰子可以到达终点,到达蛇头就回到蛇尾,到达梯子底部就上升到梯子顶部。
思路:
BFS,到达每一个节点都可以扩展出6个节点,判断终点是不是到达过,如果到达过,退出循环。
这题WA了……
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cctype> 6 #include <stack> 7 #include <queue> 8 #include <map> 9 #include <set> 10 #include <vector> 11 #include <cmath> 12 #include <algorithm> 13 #define lson l, m, rt<<1 14 #define rson m+1, r, rt<<1|1 15 using namespace std; 16 typedef long long int LL; 17 const int MAXN = 0x3f3f3f3f; 18 const int MIN = -0x3f3f3f3f; 19 const double eps = 1e-9; 20 const int dir[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{-1,1}, 21 {1,1},{1,-1},{-1,-1}}; 22 int D, N , S, L, a[23*23], b[23*23]; 23 typedef struct La{ 24 int start, end; 25 }La; 26 La la[120]; 27 int main(void){ 28 #ifndef ONLINE_JUDGE 29 freopen("arb.in", "r", stdin); 30 #endif 31 scanf("%d", &D); 32 while (D--){ 33 scanf("%d%d%d", &N, &S, &L); int i , j, k, s; 34 for (i = 1; i <= S + L; ++i){ 35 scanf("%d%d", &la[i].start, &la[i].end); 36 } memset(a, 0, sizeof(a)); 37 memset(b, 0, sizeof(b)); b[1] = 1; 38 int cnt = 0; bool flag = false; 39 while (a[N*N] == 0){ 40 memcpy(a, b, sizeof(b)); 41 memset(b, 0, sizeof(b)); 42 for (i = 1; i < N*N; ++i){ 43 if (!a[i]) continue; 44 for (j = 1; j <= 6; ++j){ 45 if (j + i > N*N) break; 46 flag = false; 47 for (k = 1; k <= L + S; ++k){ 48 if (j + i == la[k].start){ 49 b[la[k].end] = 1; flag = true; 50 } 51 } 52 if (!flag && !b[i+j]) b[i+j] = 1; 53 } 54 } cnt++; 55 } 56 cout << cnt-1 << endl; 57 } 58 59 return 0; 60 }
交了,WA……