弱菜做了好久23333333........
传送门:
A 只能摆k个棋子,只能摆在#
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 using namespace std; 5 const int max_n = 10; 6 char maze[max_n][max_n]; 7 bool vis[max_n]; 8 int n, m, ans, cnt; 9 void init(){10 memset(maze, 0, sizeof(maze));11 ans = cnt = 0;12 }13 void dfs(int k){14 if(cnt == m){ ++ans; return ; }15 if(k > n) return ;16 for(int i = 0; i < n; ++i){17 if(maze[i][k] == '#' && !vis[i]){18 vis[i] = 1; ++cnt;19 dfs(k+1);20 vis[i] = 0; --cnt;21 }22 }23 dfs(k+1);24 }25 int main(){26 ios::sync_with_stdio(false);27 while(cin >> n >> m){28 if(n == m && n == -1) break;29 init();30 for(int i = 0; i < n; ++i){31 cin.get();32 for(int j = 0; j < n; ++j) cin >> maze[i][j];33 }34 dfs(0);35 cout << ans << endl;36 }37 return 0;38 }
B 三维BFS
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 const int max_n = 40; 8 const int cx[] = { 0, 1, 0, -1, 0, 0}; 9 const int cy[] = { 1, 0, -1, 0, 0, 0};10 const int cz[] = { 0, 0, 0, 0, 1, -1};11 int maze[max_n][max_n][max_n];12 bool vis[max_n][max_n][max_n];13 struct point{14 int x, y, z, step;15 };16 int L, R, C;17 int ex, ey, ez;18 int main(){19 ios::sync_with_stdio(false);20 while(cin >> L >> R >> C){21 if(L == 0 && R == 0 && C == 0) break;22 queue q;23 memset(vis, 0, sizeof(vis));24 memset(maze, 0, sizeof(maze));25 for(int k = 0; k < L; ++k){26 for(int i = 0; i < R; ++i){27 cin.get();28 for(int j = 0; j < C; ++j){29 char ch = cin.get();30 if(ch == 'S'){31 maze[k][i][j] = 1;32 point p = {k, i, j, 0};33 q.push(p);34 }35 else if(ch == '.') maze[k][i][j] = 1;36 else if(ch == 'E'){37 maze[k][i][j] = 1;38 ex = k; ey = i; ez = j;39 }40 }41 }42 cin.get();43 }44 int x, y, z, step;45 while(!q.empty()){46 point p = q.front();47 q.pop();48 x = p.x, y = p.y, z = p.z, step = p.step;49 if(x == ex && y == ey && z == ez) break;50 for(int i = 0; i < 6; ++i){51 int nx = x + cx[i], ny = y + cy[i], nz = z + cz[i];52 if(nx<0||L <0||R <0||C
C 搜......
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 struct point{ 8 int x, step; 9 };10 int S, E;11 int vis[100010];12 int main(){13 ios::sync_with_stdio(false);14 while(cin >> S >> E){15 memset(vis, 0, sizeof(vis));16 vis[S] = 1;17 queue q;18 q.push(point{S, 0});19 int x, step;20 while(!q.empty()){21 point p = q.front();22 x = p.x; step = p.step;23 q.pop();24 if(x == E) break;25 for(int i = 0; i < 3; ++i){26 int nx;27 if(i == 0) nx = x + 1;28 else if(i == 1) nx = x - 1;29 else nx = x << 1;30 if(0 <= nx && nx <= 100000 && !vis[nx]){ q.push(point{nx, step+1}); vis[nx] = 1; }31 }32 }33 cout << step << endl;34 }35 return 0;36 }
D 卡了好久看了题解 第一行确定下来后其他行的也都确定下来了(说的好有道理),所以枚举第一行状态,确定下其他行,看最后一行是否都是0
然后在这道题发现了之前二进制枚举的一个错误....... 正确姿势:(i>>j) & 1(吧?)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 8 const int MAXN = 20; 9 const int INF = 0x3f3f3f3f;10 const int cx[] = { 0, 0, -1, 0};11 const int cy[] = { 1, -1, 0, 0};12 int n, m;13 int ans[MAXN][MAXN];14 int flip[MAXN][MAXN];15 int origin[MAXN][MAXN];16 17 int check(int x, int y){18 int cnt = origin[x][y];19 for(int i = 0; i < 4; ++i){20 int nx = x + cx[i], ny = y + cy[i];21 if(nx >= 0 && nx < n && ny >= 0 && ny < m)22 cnt += flip[nx][ny];23 }24 return cnt & 1;25 }26 27 int duang(){28 for(int i = 1; i < n; ++i)29 for(int j = 0; j < m; ++j)30 if(check(i-1, j)) flip[i][j] = 1;31 for(int j = 0; j < m; ++j)32 if(check(n-1, j)) return 0;33 int cnt = 0;34 for(int i = 0; i < n; ++i)35 for(int j = 0; j < m; ++j)36 cnt += flip[i][j];37 return cnt;38 }39 40 int main(){41 while(cin >> n >> m){42 memset(origin, 0, sizeof(origin));43 for(int i = 0; i < n; ++i)44 for(int j = 0; j < m; ++j)45 cin >> origin[i][j];46 int tans = INF;47 for(int i = 0; i < (1< >j) & 1;51 int cnt = duang();52 if(cnt < tans && cnt != 0){53 tans = cnt;54 memcpy(ans, flip, sizeof(flip));55 }56 }57 if(tans == INF) cout << "IMPOSSIBLE" << endl;58 else{59 for(int i = 0; i < n; ++i)60 for(int j = 0; j < m; ++j)61 cout << ans[i][j] << " \n"[j==m-1];62 }63 }64 return 0;65 }
E 听说答案都在long long范围内hhhh.......
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 using namespace std; 5 typedef long long ll; 6 int main(){ 7 ios::sync_with_stdio(false); 8 int n; 9 while(cin >> n){10 if(n == 0) break;11 queue q;12 q.push(1LL);13 while(!q.empty()){14 ll k = q.front();15 q.pop();16 if(k % n == 0){17 cout << k << endl;18 break;19 }20 q.push(k*10);21 q.push(k*10+1);22 }23 }24 return 0;25 }
F GOJ有这道题........
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include
G 水 直接模拟 略坑是第一张都是最底的一张
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 7 8 int main(){ 9 int t;10 cin >> t;11 for(int x = 1; x <= t; ++x){12 int l;13 cin >> l;14 string str1, str2, desstr;15 cin >> str1 >> str2 >> desstr;16 int step = 0;17 set s;18 s.insert(str1+str2);19 bool flag = true;20 while(flag){21 string tmpstr;22 for(int i = 0; i < l; ++i){23 tmpstr += str2[i];24 tmpstr += str1[i];25 }26 //cout << tmpstr << endl;27 ++step;28 if(tmpstr == desstr) break;29 if(s.count(tmpstr)){30 flag = false;31 break;32 }33 s.insert(tmpstr);34 str1 = tmpstr.substr(0, l);35 str2 = tmpstr.substr(l, 2*l);36 //cout << str1 << " " << str2 << endl;37 }38 cout << x << ' ' << (flag ? step : -1) << endl;39 }40 return 0;41 }
H M题写吐了H不想做.......
还是搜 6种状态
I 暴力找两个#搜.......
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 8 const int INF = 0x3f3f3f; 9 const int cx[4] = { 0, 0, 1, -1};10 const int cy[4] = { 1, -1, 0, 0};11 int n, m;12 int dis[15][15];13 char map[15][15];14 15 struct Point{16 int x, y;17 Point(){}18 Point(int xx, int yy) :x(xx), y(yy){}19 };20 queue q;21 int bfs(int x1, int y1, int x2, int y2){22 memset(dis, INF, sizeof(dis));23 q.push(Point(x1, y1));24 q.push(Point(x2, y2));25 dis[x1][y1] = 0;26 dis[x2][y2] = 0;27 while (!q.empty()){28 Point p = q.front();29 q.pop();30 for (int i = 0; i < 4; i++){31 int xx = p.x + cx[i], yy = p.y + cy[i];32 if (xx >= 0 && xx < n && yy >= 0 && yy dis[p.x][p.y] + 1){33 dis[xx][yy] = dis[p.x][p.y] + 1;34 q.push(Point(xx, yy));35 }36 }37 }38 39 int ret = 0;40 for (int i = 0; i < n; i++)41 for (int j = 0; j < m; j++)42 if (map[i][j] == '#')43 ret = max(ret, dis[i][j]);44 return ret;45 }46 47 int main(){48 int t;49 cin >> t;50 for (int x = 1; x <= t; x++){51 while(!q.empty()) q.pop();52 cin >> n >> m;53 for (int i = 0; i < n; i++)54 cin >> map[i];55 int ans = INF;56 for (int i = 0; i < n; i++)57 for (int j = 0; j < m; j++)58 if (map[i][j] == '#')59 for (int ii = 0; ii < n; ii++)60 for (int jj = 0; jj < m; jj++)61 if (map[ii][jj] == '#'){62 int temp = bfs(i, j, ii, jj);63 ans = min(ans, temp);64 }65 if (ans == INF) ans = -1;66 cout << "Case " << x << ": " << ans << endl;67 }68 return 0;69 }
J 多处火.....先处理火,再处理人,遇到可行的情况退出来
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 using namespace std; 3 4 struct pos{ 5 int x, y; 6 pos(int xx = 0, int yy = 0): x(xx), y(yy) {} 7 }; 8 const int MAXN = 1111; 9 const int INF = 0x7f7f7f7f;10 const int cx[] = { 0, 0, 1, -1};11 const int cy[] = { 1, -1, 0, 0};12 int n, m;13 int jx, jy, fx, fy;14 int maze[MAXN][MAXN];15 int jstep[MAXN][MAXN];16 int fstep[MAXN][MAXN];17 18 int main(){19 int t;20 cin >> t;21 while(t--){22 cin >> n >> m;23 queue qf;24 memset(fstep, -1, sizeof(fstep));25 for(int i = 0; i < n; ++i){26 cin.get();27 for(int j = 0; j < m; ++j){28 char ch;29 cin >> ch;30 if(ch == '#') maze[i][j] = 0;31 else{32 maze[i][j] = 1;33 if(ch == 'J'){34 jx = i; 35 jy = j;36 }37 else if(ch == 'F'){38 fx = i;39 fy = j;40 qf.push(pos(fx, fy));41 fstep[fx][fy] = 0;42 }43 }44 }45 }46 while(!qf.empty()){47 pos p = qf.front();48 int x = p.x, y = p.y;49 qf.pop();50 for(int i = 0; i < 4; ++i){51 int nx = x + cx[i], ny = y + cy[i];52 if(nx < 0 || n <= nx || ny < 0 || m <= ny || maze[nx][ny] == 0 || fstep[nx][ny] != -1) continue;53 qf.push(pos(nx, ny));54 fstep[nx][ny] = fstep[x][y] + 1;55 }56 }57 // cout << endl;58 // for(int i = 0; i < n; ++i){59 // for(int j = 0; j < m; ++j)60 // cout << fstep[i][j] << '\t';61 // cout << endl;62 // }63 memset(jstep, -1, sizeof(jstep));64 queue qj;65 qj.push(pos(jx, jy));66 jstep[jx][jy] = 0;67 int ans = -1;68 while(!qj.empty()){69 pos p = qj.front();70 int x = p.x, y = p.y;71 qj.pop();72 if(x == 0 || x == n-1 || y == 0 || y == m-1){73 ans = jstep[x][y];74 break;75 }76 for(int i = 0; i < 4; ++i){77 int nx = x + cx[i], ny = y + cy[i];78 if(nx < 0 || n <= nx || ny < 0 || m <= ny || maze[nx][ny] == 0 || jstep[nx][ny] != -1 || (fstep[nx][ny] != -1 && fstep[nx][ny] <= jstep[x][y] + 1)) continue;79 qj.push(pos(nx, ny));80 jstep[nx][ny] = jstep[x][y] + 1;81 }82 }83 // cout << endl;84 // for(int i = 0; i < n; ++i){85 // for(int j = 0; j < m; ++j)86 // cout << jstep[i][j] << '\t';87 // cout << endl;88 // }89 if(ans == -1) cout << "IMPOSSIBLE" << endl;90 else cout << ans+1 << endl;91 }92 return 0;93 }
K 搜 开数组记录上一个点的坐标
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include
L 数水坑类的 8个方向
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 using namespace std; 5 int ans; 6 char maze[120][120]; 7 const int cx[] = { 0, 0, 1, 1, 1, -1, -1, -1}; 8 const int cy[] = { 1, -1, 1, -1, 0, 1, 0, -1}; 9 void dfs(int x, int y){10 maze[x][y] = '*';11 for(int i = 0; i < 8; ++i){12 int nx = x + cx[i], ny = y + cy[i];13 if(maze[nx][ny] == '@') dfs(nx, ny);14 }15 }16 int main(){17 ios::sync_with_stdio(false);18 int m, n;19 while(cin >> m >> n){20 if(m == 0 && n == 0) break;21 ans = 0;22 for(int i = 0; i < m; ++i){23 cin.get();24 for(int j = 0; j < n; ++j)25 cin >> maze[i][j];26 }27 for(int i = 0; i < m; ++i){28 for(int j = 0; j < n; ++j){29 if(maze[i][j] == '@'){30 ++ans;31 dfs(i, j);32 }33 }34 }35 cout << ans << endl;36 }37 return 0;38 }
M 可乐.....改了好久蜜汁哇 果断重写 简直恶心
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 using namespace std; 3 //#define print() cout< <<" "< <<" "< < q; 11 const int MAXN = 111; 12 int step[MAXN][MAXN][MAXN]; 13 14 int main(){ 15 while(cin >> s >> n >> m){ 16 if(s == 0 && n == 0 && m == 0) break; 17 if(s & 1) cout << "NO" << endl; 18 else{ 19 while(!q.empty()) q.pop(); 20 memset(step, -1, sizeof(step)); 21 int ans = -1; 22 q.push(state(s, 0, 0)); 23 step[s][0][0] = 0; 24 while(!q.empty()){ 25 state p = q.front(); 26 q.pop(); 27 int ss = p.s, nn = p.n, mm = p.m; 28 if((ss == s/2 && nn == s/2) || (ss == s/2 && mm == s/2) || (nn == s/2 && mm == s/2)){ 29 ans = step[ss][nn][mm]; 30 break; 31 } 32 state tmp; 33 //s -> n || s -> m 34 if(ss){ 35 //s -> n 36 if(ss > n - nn){ 37 tmp.s = ss - (n - nn); 38 tmp.n = n; 39 tmp.m = mm; 40 } 41 else{ 42 tmp.s = 0; 43 tmp.n = nn + ss; 44 tmp.m = mm; 45 } 46 if(step[tmp.s][tmp.n][tmp.m] == -1){ 47 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 48 q.push(tmp); 49 } 50 //ss -> m 51 if(ss > m - mm){ 52 tmp.s = ss - (m - mm); 53 tmp.m = m; 54 tmp.n = nn; 55 } 56 else{ 57 tmp.s = 0; 58 tmp.m = mm + ss; 59 tmp.n = nn; 60 } 61 if(step[tmp.s][tmp.n][tmp.m] == -1){ 62 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 63 q.push(tmp); 64 } 65 } 66 //n -> s || n -> m 67 if(nn){ 68 //n -> s 69 if(nn > s - ss){ 70 tmp.n = nn - (s - ss); 71 tmp.s = s; 72 tmp.m = mm; 73 } 74 else{ 75 tmp.n = 0; 76 tmp.s = ss + nn; 77 tmp.m = mm; 78 } 79 if(step[tmp.s][tmp.n][tmp.m] == -1){ 80 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 81 q.push(tmp); 82 } 83 //n -> m 84 if(nn > m - mm){ 85 tmp.n = nn - (m - mm); 86 tmp.m = m; 87 tmp.s = ss; 88 } 89 else{ 90 tmp.n = 0; 91 tmp.m = mm + nn; 92 tmp.s = ss; 93 } 94 if(step[tmp.s][tmp.n][tmp.m] == -1){ 95 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 96 q.push(tmp); 97 } 98 } 99 //m -> n || m -> s100 if(mm){101 //m -> n102 if(mm > n - nn){103 tmp.m = mm - (n - nn);104 tmp.n = n;105 tmp.s = ss;106 }107 else{108 tmp.m = 0;109 tmp.n = nn + mm;110 tmp.s = ss;111 }112 if(step[tmp.s][tmp.n][tmp.m] == -1){113 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1;114 q.push(tmp);115 }116 //m -> s117 if(mm > s - ss){118 tmp.m = mm - (s - ss);119 tmp.s = s;120 tmp.n = nn;121 }122 else{123 tmp.m = 0;124 tmp.s = mm + ss;125 tmp.n = nn;126 }127 if(step[tmp.s][tmp.n][tmp.m] == -1){128 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1;129 q.push(tmp);130 }131 }132 }133 if(ans == -1) cout << "NO" << endl;134 else cout << ans << endl;135 }136 }137 return 0;138 }
N 从两个人的位置开始bfs整个图
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 10 struct pos{11 int x, y;12 pos(int xx, int yy): x(xx), y(yy){};13 };14 const int MAXN = 222;15 char maze[MAXN][MAXN];16 const int cx[] = { 1, -1, 0, 0};17 const int cy[] = { 0, 0, 1, -1};18 vector kfc;19 queue qy; int yx, yy, stepy[MAXN][MAXN];20 queue qm; int mx, my, stepm[MAXN][MAXN];21 22 int main(){23 int n, m;24 while(cin >> n >> m){25 kfc.clear();26 for(int i = 0; i < n; ++i){27 cin.get();28 for(int j = 0; j < m; ++j){29 char ch;30 cin >> ch;31 if(ch == '#') maze[i][j] = ch;32 else{33 if(ch == 'Y'){34 yx = i;35 yy = j;36 }37 if(ch == 'M'){38 mx = i;39 my = j;40 }41 if(ch == '@')42 kfc.push_back(pos(i, j));43 maze[i][j] = '.';44 }45 }46 }47 while(!qy.empty()) qy.pop();48 while(!qm.empty()) qm.pop();49 memset(stepy, -1, sizeof(stepy));50 memset(stepm, -1, sizeof(stepm));51 qy.push(pos(yx, yy));52 stepy[yx][yy] = 0;53 while(!qy.empty()){54 int x = qy.front().x, y = qy.front().y;55 qy.pop();56 for(int i = 0; i < 4; ++i){57 int nx = x + cx[i], ny = y + cy[i];58 if(nx < 0 || n < nx || ny < 0 || m < ny || maze[nx][ny] == '#' || stepy[nx][ny] != -1) continue;59 stepy[nx][ny] = stepy[x][y] + 1;60 qy.push(pos(nx, ny));61 }62 }63 qm.push(pos(mx, my));64 stepm[mx][my] = 0;65 while(!qm.empty()){66 int x = qm.front().x, y = qm.front().y;67 qm.pop();68 for(int i = 0; i < 4; ++i){69 int nx = x + cx[i], ny = y + cy[i];70 if(nx < 0 || n < nx || ny < 0 || m < ny || maze[nx][ny] == '#' || stepm[nx][ny] != -1) continue;71 stepm[nx][ny] = stepm[x][y] + 1;72 qm.push(pos(nx, ny));73 }74 }75 int ans = 0x7f7f7f7f;76 for(vector ::iterator i = kfc.begin(); i != kfc.end(); ++i){77 int x = (*i).x, y = (*i).y;78 ans = min(ans, stepy[x][y] + stepm[x][y]);79 }80 cout << ans*11 << endl;81 }82 return 0;83 }
.............................................................................