前期天胡开局各种1A,然后被弱智E题卡到集体降智。后面的题也太难了8
7题滚了,过于真实
A:
solver:zyh
1 #include <bits/stdc++.h> 2 #include <unordered_set> 3 using namespace std; 4 struct board { 5 int arr[6][6]; 6 board() { 7 memset(arr, 0, sizeof(arr)); 8 } 9 }; 10 //EW:1 NS:2 11 int towards[11]; 12 int len[11]; 13 struct state { 14 int x[11]; 15 int y[11]; 16 bool operator==(const state &b)const { 17 for (int i = 1; i <= 10; ++i) if (x[i] != b.x[i] || y[i] != b.y[i]) return false; 18 return true; 19 } 20 }; 21 struct myHash { 22 size_t operator()(state State) const { 23 size_t hash = 0; 24 for (int i = 1; i <= 10; ++i) { 25 hash = hash * 7 + size_t(State.x[i]); 26 hash = hash * 7 + size_t(State.y[i]); 27 } 28 return hash; 29 } 30 }; 31 board st; 32 state st_state; 33 queue< pair<int, state> > que; 34 unordered_set<state, myHash> Set; 35 board generateBoard(state State) { 36 board rnt; 37 for (int i = 1; i <= 10; ++i) 38 if (towards[i] > 0) { 39 int dx = 0, dy = 0; 40 int x = State.x[i]; 41 int y = State.y[i]; 42 if (towards[i] == 1) dy = 1; 43 else dx = 1; 44 for (int j = 0; j < len[i]; ++j) { 45 rnt.arr[x][y] = i; 46 x += dx; y += dy; 47 } 48 } 49 return rnt; 50 } 51 void output(int s, state State) { 52 //return; 53 cout << "s=" << s << endl; 54 board Board = generateBoard(State); 55 for (int i = 0; i < 6; ++i) { 56 for (int j = 0; j < 6; ++j) cout << Board.arr[i][j] << ' '; 57 cout << endl; 58 } 59 system("pause"); 60 } 61 int bfs() { 62 que.push(pair<int, state>(0, st_state)); 63 Set.insert(st_state); 64 while (!que.empty()) { 65 auto tmp = que.front(); 66 int nows = tmp.first; 67 state nowstate = tmp.second; 68 que.pop(); 69 if (nows == 10) continue; 70 board nowboard = generateBoard(nowstate); 71 for (int i = 1; i <= 10; ++i) { 72 //positive 73 if (towards[i] == 0) continue; 74 state tmpstate = nowstate; 75 int dx = 0, dy = 0; 76 if (towards[i] == 1) dy = 1; 77 else dx = 1; 78 for (int j = 1; j <= 1; ++j) { 79 //positive 80 if (nowstate.x[i] + dx * (len[i] + j - 1) < 6 && 81 nowstate.y[i] + dy * (len[i] + j - 1) < 6 && 82 nowboard.arr[nowstate.x[i] + dx * (len[i] + j - 1)][nowstate.y[i] + dy * (len[i] + j - 1)] == 0) { 83 tmpstate.x[i] = nowstate.x[i] + dx * j; 84 tmpstate.y[i] = nowstate.y[i] + dy * j; 85 if (Set.find(tmpstate) == Set.end()) { 86 if (tmpstate.y[1] + len[1] - 1 == 5) { 87 //output(nows+1,tmpstate); 88 if (nows + 1 + len[1] <= 10) return nows + 1 + len[1]; 89 return -1; 90 } 91 //output(nows+1,tmpstate); 92 que.push(pair<int, state>(nows + 1, tmpstate)); 93 Set.insert(tmpstate); 94 } 95 } else break; 96 } 97 for (int j = 1; j <= 1; ++j) { 98 if (nowstate.x[i] - dx * j >= 0 && 99 nowstate.y[i] - dy * j >= 0 && 100 nowboard.arr[nowstate.x[i] - dx * j][nowstate.y[i] - dy * j] == 0) { 101 tmpstate.x[i] = nowstate.x[i] - dx * j; 102 tmpstate.y[i] = nowstate.y[i] - dy * j; 103 if (Set.find(tmpstate) == Set.end()) { 104 if (tmpstate.y[1] + len[1] - 1 == 5) { 105 //output(nows+1,tmpstate); 106 if (nows + 1 + len[1] <= 10) return nows + 1 + len[1]; 107 return -1; 108 } 109 //output(nows+1,tmpstate); 110 111 que.push(pair<int, state>(nows + 1, tmpstate)); 112 Set.insert(tmpstate); 113 } 114 } else break; 115 } 116 } 117 } 118 return -1; 119 } 120 int main() { 121 for (int i = 0; i < 6; ++i) 122 for (int j = 0; j < 6; ++j) scanf("%d", &st.arr[i][j]); 123 for (int i = 0; i < 6; ++i) 124 for (int j = 0; j < 6; ++j) 125 if (st.arr[i][j] > 0) { 126 int t = st.arr[i][j]; 127 if (towards[t] > 0) continue; 128 st_state.x[t] = i; 129 st_state.y[t] = j; 130 int dx = 0, dy = 0; 131 if (j + 1 < 6 && st.arr[i][j + 1] == t) { 132 towards[t] = 1; 133 dy = 1; 134 } else { 135 towards[t] = 2; 136 dx = 1; 137 } 138 int x = i, y = j; 139 len[t] = 0; 140 while (st.arr[x][y] == t) { 141 len[t]++; 142 x += dx; y += dy; 143 } 144 } 145 if (st_state.x[1] != 2 || towards[1] != 1) { 146 printf("-1\n"); 147 return 0; 148 } 149 printf("%d", bfs()); 150 }