20171007上 T2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20171007上 T2相关的知识,希望对你有一定的参考价值。
MMP , 不判加冕我能过啊
B 国际跳棋
文件名 输入文件 输出文件 时间限制 空间限制
chess.cpp/c/pas chess.in chess.out 1s 512MB
题目描述
国际跳棋是一种古老的棋类游戏,远在古埃及法老时期就已存在,现代国际跳
棋是在 12 世纪定型的。国际跳棋是由各国的民族跳棋演变而来,其历史源远流长。
简化版(与标准国际跳棋略有差别)国际跳棋规则:
? 国际跳棋的棋盘由 10 × 10 共 100 个格子组成
? 初始的时候,黑白双方各有 20 个棋子
? 移动:可以将我方任意棋子向左前方或右前方移动 1 步
? 跳吃:只要左前方、右前方、左后方、右后方相邻格子有对方棋子,且跳过这
枚对方棋子后有空位,则可以跳过对方棋子并将对方棋子吃掉。如你的棋子在
(x,y), 对方棋子在 (x+1,y+1), (x+2,y+2) 为空, 则你可以跳到 (x+2,y+2)
并吃掉对方的棋子
? 加冕: 任何一个棋子在行动过程停止的时候停到了对方底线(最靠近对方的一
行)就可以加冕,从此成为“王”。注意,连续跳吃的时候只有最后一步停在对
方底线才可以加冕
? 连跳:跳吃可以由多次跳吃组成。
? 王的特权:王在移动的时候可以无视方向(左前、右前、左后、右后都可以) ,
无视距离(走几步都行, 直到遇到别的棋子) , 无视跳吃距离(比如说 (x,y) 跳
过 (x + 3,y + 3) 落到 (x + 7,y + 7) 是可以的,但是这中间除了有被吃掉的对
方棋子,不能有其他棋子
? 在跳吃结束的时候才将被吃掉的棋子拿出棋盘,在这之前作为“屏障”,即这些
棋子不能再次被跳吃,也不能落子
? 按照以上规则,给定一个棋局,合法的操作方案有很多。然而,每次必须选择
吃子最多的操作方案。比如,在某种棋局下,有 A、B、C、D 四种方案,A、
B 吃子 3 枚,C 吃 1 枚,D 吃 0 枚,则真正合法的操作总数为 2
作为一个国际跳棋迷,陶陶想要编写一个网络对战跳棋软件。然而他现在不会
判断怎样的操作是合法的。对于给定的局面,你能给出所有合法的操作吗?
输入格式
输入数据是两个十行十列的矩阵,第一个矩阵中的每个点可能是以下三种:
? 0 空位置
? 1 我方棋子
? 2 对方棋子
第二个矩阵描述的是国王的情况。若为 1,表示是国王;为 0 表示不是国王。
输出格式
输出第一行为一个数字,表示合法操作的个数 ans。
下面一共 ans 行,每行表示一种合法操作中被操作的棋子。格式为 (x,y) 表示
该棋子在第 x 行、第 y 列(注意,逗号后面没有空格) 。如果某一个棋子有多种合
法操作,则输出多遍。输出的顺序按从上到下、从左到右。
如果没有任何合法操作,只输出一个 0 即可
样例输入 1
0000000000
0000100000
0000000200
0000100000
0000000200
0000001000
0000000200
2000000000
0101000200
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
样例输出 1
2
(6,7)
(6,7)
#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <cstring> #define rg register inline void read (int &n) { rg char c = getchar (); for (n = 0; !isdigit (c); c = getchar ()); for (; isdigit (c); n = n * 10 + c - ‘0‘, c = getchar ()); } #define Max 13 #define N 10 char data[Max + 3][Max + 3]; int map[Max + 3][Max + 3]; struct D { int x, y; D (int _x = 0, int _y = 0) : x (_x), y (_y) {} bool operator < (const D &A) const { return x == A.x ? y < A.y : x < A.x; } }; std :: vector <D> s[N * 3]; int res, X, Y; bool IsGamn (int x, int y) { return x == 1; } void Move (int x, int y) { if (map[x - 1][y - 1] == 2) s[1].push_back (D (X, Y)); if (map[x - 1][y + 1] == 2) s[1].push_back (D (X, Y)); } void Jump (int x, int y, int r) { if (IsGamn (x, y)) { rg int i, j; for (i = 1; i <= N; ++ i) { if (map[x + i][y + i] == -1 || map[x + i][y + i] == 4) break; if (map[x + i][y + i] == 2) for (j = 1; j <= N; ++ j) if (map[x + i + j][y + i + j] == 0) map[x + i][y + i] = 4, Jump (x + i + j, y + i + j, r + 1), map[x + i][y + i] = 2; else break; } for (i = 1; i <= N; ++ i) { if (map[x + i][y - i] == -1 || map[x + i][y - i] == 4) break; if (map[x + i][y - i] == 2) for (j = 1; j <= N; ++ j) if (map[x + i + j][y - i - j] == 0) map[x + i][y - i] = 4, Jump (x + i + j, y - i - j, r + 1), map[x + i][y - i] = 2; else break; } } if (map[x + 1][y + 1] == 2 && map[x + 2][y + 2] == 0) map[x + 1][y + 1] = 4, Jump (x + 2, y + 2, r + 1), map[x + 1][y + 1] = 2; if (map[x + 1][y - 1] == 2 && map[x + 2][y - 2] == 0) map[x + 1][y - 1] = 4, Jump (x + 2, y - 2, r + 1), map[x + 1][y - 1] = 2; if (map[x - 1][y + 1] == 2 && map[x - 2][y + 2] == 0) map[x - 1][y + 1] = 4, Jump (x - 2, y + 2, r + 1), map[x - 1][y + 1] = 2; if (map[x - 1][y - 1] == 2 && map[x - 2][y - 2] == 0) map[x - 1][y - 1] = 4, Jump (x - 2, y - 2, r + 1), map[x - 1][y - 1] = 2; if (r) s[r].push_back (D (X, Y)); } void KingWalk (int x, int y) { rg int i; for (i = 1; i <= N; ++ i) if (map[x + i][y + i] == 2) { s[1].push_back (D (X, Y)); break; } for (i = 1; i <= N; ++ i) if (map[x + i][y - i] == 2) { s[1].push_back (D (X, Y)); break; } for (i = 1; i <= N; ++ i) if (map[x - i][y + i] == 2) { s[1].push_back (D (X, Y)); break; } for (i = 1; i <= N; ++ i) if (map[x - i][y - i] == 2) { s[1].push_back (D (X, Y)); break; } } void Spmak (int x, int y, int r) { if (x < 1 || x > N || y < 1 || y > N) return ; rg int i, j; for (i = 1; i <= N; ++ i) { if (map[x + i][y + i] == -1 || map[x + i][y + i] == 4) break; if (map[x + i][y + i] == 2) for (j = 1; j <= N; ++ j) if (map[x + i + j][y + i + j] == 0) map[x + i][y + i] = 4, Jump (x + i + j, y + i + j, r + 1), map[x + i][y + i] = 2; else break; } for (i = 1; i <= N; ++ i) { if (map[x + i][y - i] == -1 || map[x + i][y - i] == 4) break; if (map[x + i][y - i] == 2) for (j = 1; j <= N; ++ j) if (map[x + i + j][y - i - j] == 0) map[x + i][y - i] = 4, Jump (x + i + j, y - i - j, r + 1), map[x + i][y - i] = 2; else break; } for (i = 1; i <= N; ++ i) { if (map[x - i][y + i] == -1 || map[x - i][y + i] == 4) break; if (map[x - i][y + i] == 2) for (j = 1; j <= N; ++ j) if (map[x - i - j][y + i + j] == 0) map[x - i][y + i] = 4, Jump (x - i - j, y + i + j, r + 1), map[x - i][y + i] = 2; else break; } for (i = 1; i <= N; ++ i) { if (map[x - i][y - i] == -1 || map[x - i][y - i] == 4) break; if (map[x - i][y - i] == 2) for (j = 1; j <= N; ++ j) if (map[x - i - j][y - i - j] == 0) map[x - i][y - i] = 4, Jump (x - i - j, y - i - j, r + 1), map[x - i][y - i] = 2; else break; } if (r) s[r].push_back (D (X, Y)); } std :: string Name = "chess", _I = ".in", _O = ".out"; void Calc (int x, int y) { X = x, Y = y; if (map[x][y] == 3 || IsGamn (x, y)) { KingWalk (x, y); Spmak (x, y, 0); return ; } Move (x, y), Jump (x, y, 0); } int main (int argc, char *argv[]) { freopen ((Name + _I).c_str (), "r", stdin); freopen ((Name + _O).c_str (), "w", stdout); rg int i, j; memset (map, -1, sizeof map); for (i = 1; i <= N; ++ i) { scanf ("%s", data[i] + 1); for (j = 1; j <= N; ++ j) map[i][j] = data[i][j] - ‘0‘; } for (i = 1; i <= N; ++ i) { scanf ("%s", data[i] + 1); for (j = 1; j <= N; ++ j) if (data[i][j] == ‘1‘) map[i][j] = map[i][j] == 1 ? 3 : 2; } for (i = 1; i <= N; ++ i) for (j = 1; j <= N; ++ j) if (map[i][j] == 1 || map[i][j] == 3) Calc (i, j); for (i = 21; i >= 1; -- i) if (s[i].size ()) { printf ("%d\n", s[i].size ()); std :: sort (s[i].begin (), s[i].end ()); for (j = 0; j < s[i].size (); ++ j) printf ("(%d,%d)\n", s[i][j].x, s[i][j].y); return 0; } return printf ("0"), 0; }
#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <cstring> #define rg register inline void read (int &n) { rg char c = getchar (); for (n = 0; !isdigit (c); c = getchar ()); for (; isdigit (c); n = n * 10 + c - ‘0‘, c = getchar ()); } #define Max 13 #define N 10 char data[Max + 3][Max + 3]; int map[Max + 3][Max + 3]; struct D { int x, y; D (int _x = 0, int _y = 0) : x (_x), y (_y) {} bool operator < (const D &A) const { return x == A.x ? y < A.y : x < A.x; } }; std :: vector <D> s[N * 3]; int res, X, Y; bool IsGamn (int x, int y) { return x == 1; } void Move (int x, int y) { if (map[x - 1][y - 1] == 2) s[1].push_back (D (X, Y)); if (map[x - 1][y + 1] == 2) s[1].push_back (D (X, Y)); } void Jump (int x, int y, int r) { if (map[x + 1][y + 1] == 2 && map[x + 2][y + 2] == 0) map[x + 1][y + 1] = 4, Jump (x + 2, y + 2, r + 1), map[x + 1][y + 1] = 2; if (map[x + 1][y - 1] == 2 && map[x + 2][y - 2] == 0) map[x + 1][y - 1] = 4, Jump (x + 2, y - 2, r + 1), map[x + 1][y - 1] = 2; if (map[x - 1][y + 1] == 2 && map[x - 2][y + 2] == 0) map[x - 1][y + 1] = 4, Jump (x - 2, y + 2, r + 1), map[x - 1][y + 1] = 2; if (map[x - 1][y - 1] == 2 && map[x - 2][y - 2] == 0) map[x - 1][y - 1] = 4, Jump (x - 2, y - 2, r + 1), map[x - 1][y - 1] = 2; if (r) s[r].push_back (D (X, Y)); } void KingWalk (int x, int y) { rg int i; for (i = 1; i <= N; ++ i) if (map[x + i][y + i] == 2) { s[1].push_back (D (X, Y)); break; } for (i = 1; i <= N; ++ i) if (map[x + i][y - i] == 2) { s[1].push_back (D (X, Y)); break; } for (i = 1; i <= N; ++ i) if (map[x - i][y + i] == 2) { s[1].push_back (D (X, Y)); break; } for (i = 1; i <= N; ++ i) if (map[x - i][y - i] == 2) { s[1].push_back (D (X, Y)); break; } } void Spmak (int x, int y, int r) { if (x < 1 || x > N || y < 1 || y > N) return ; rg int i, j; for (i = 1; i <= N; ++ i) { if (map[x + i][y + i] == -1 || map[x + i][y + i] == 4) break; if (map[x + i][y + i] == 2) for (j = 1; j <= N; ++ j) if (map[x + i + j][y + i + j] == 0) map[x + i][y + i] = 4, Jump (x + i + j, y + i + j, r + 1), map[x + i][y + i] = 2; else break; } for (i = 1; i <= N; ++ i) { if (map[x + i][y - i] == -1 || map[x + i][y - i] == 4) break; if (map[x + i][y - i] == 2) for (j = 1; j <= N; ++ j) if (map[x + i + j][y - i - j] == 0) map[x + i][y - i] = 4, Jump (x + i + j, y - i - j, r + 1), map[x + i][y - i] = 2; else break; } for (i = 1; i <= N; ++ i) { if (map[x - i][y + i] == -1 || map[x - i][y + i] == 4) break; if (map[x - i][y + i] == 2) for (j = 1; j <= N; ++ j) if (map[x - i - j][y + i + j] == 0) map[x - i][y + i] = 4, Jump (x - i - j, y + i + j, r + 1), map[x - i][y + i] = 2; else break; } for (i = 1; i <= N; ++ i) { if (map[x - i][y - i] == -1 || map[x - i][y - i] == 4) break; if (map[x - i][y - i] == 2) for (j = 1; j <= N; ++ j) if (map[x - i - j][y - i - j] == 0) map[x - i][y - i] = 4, Jump (x - i - j, y - i - j, r + 1), map[x - i][y - i] = 2; else break; } if (r) s[r].push_back (D (X, Y)); } std :: string Name = "chess", _I = ".in", _O = ".out"; void Calc (int x, int y) { X = x, Y = y; if (map[x][y] == 3 || IsGamn (x, y)) { KingWalk (x, y); Spmak (x, y, 0); return ; } Move (x, y), Jump (x, y, 0); } int main (int argc, char *argv[]) { freopen ((Name + _I).c_str (), "r", stdin); freopen ((Name + _O).c_str (), "w", stdout); rg int i, j; memset (map, -1, sizeof map); for (i = 1; i <= N; ++ i) { scanf ("%s", data[i] + 1); for (j = 1; j <= N; ++ j) map[i][j] = data[i][j] - ‘0‘; } for (i = 1; i <= N; ++ i) { scanf ("%s", data[i] + 1); for (j = 1; j <= N; ++ j) if (data[i][j] == ‘1‘) map[i][j] = map[i][j] == 1 ? 3 : 2; } for (i = 1; i <= N; ++ i) for (j = 1; j <= N; ++ j) if (map[i][j] == 1 || map[i][j] == 3) Calc (i, j); for (i = 21; i >= 1; -- i) if (s[i].size ()) { printf ("%d\n", s[i].size ()); std :: sort (s[i].begin (), s[i].end ()); for (j = 0; j < s[i].size (); ++ j) printf ("(%d,%d)\n", s[i][j].x, s[i][j].y); return 0; } return printf ("0"), 0; }
以上是关于20171007上 T2的主要内容,如果未能解决你的问题,请参考以下文章
为啥代码片段在 matplotlib 2.0.2 上运行良好,但在 matplotlib 2.1.0 上引发错误