CodeForces 362A - Two Semiknights Meet
2015-07-27 12:48
302 查看
A boy Petya loves chess very much. He even came up with a chess piece ofhis own, a semiknight. The semiknight can move in any of these four directions: 2 squares
forward and 2 squares to the right, 2 squares
forward and 2 squares to the left, 2squaresbackward
and 2 to the right and 2 squares
backward and 2 to the left. Naturally, the semiknight cannotmove beyond the limits of the chessboard.
Petya put two semiknights on a standard chessboard. Petya simultaneouslymoves with both semiknights. The squares are rather large, so after some movethe semiknights can meet, that is, they can end up in the same square. Afterthe
meeting the semiknights can move on, so it is possible that they meetagain. Petya wonders if there is such sequence of moves when the semiknightsmeet. Petya considers some squares bad. That is, they do not suit for themeeting. The semiknights can move through
these squares but their meetings inthese squares don't count.
Petya prepared multiple chess boards. Help Petya find out whether thesemiknights can meet on some good square for each board.
Please see the test case analysis.
Input
The first line contains number t (1 ≤ t ≤ 50)
— the number of boards. Eachboard is described by a matrix of characters, consisting of 8 rows and 8columns. The matrix consists of characters ".", "#",
"K", representing an empty good square, a bad square and thesemiknight's position, correspondingly. It is guaranteed that matrix containsexactly 2 semiknights.
The semiknight's squares areconsidered good for the meeting. The tests are separated by empty line.
Output
For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO"
otherwise.
Sample test(s)
input
2
........
........
......#.
K..##..#
.......#
...##..#
......#.
K.......
........
........
..#.....
..#..#..
..####..
...##...
........
....K#K#
output
YES
NO
Note
Consider the first board from the sample. We will assume the rows andcolumns of the matrix to be numbered 1 through 8 from top to bottom and fromleft to right, correspondingly. The knights can meet, for example, in square (2, 7).
Thesemiknight from square (4, 1) goes tosquare (2, 3)
and thesemiknight goes from square (8, 1) tosquare (6, 3).
Thenboth semiknights go to (4,5) but thissquare is bad, so they move together to square (2, 7).
On the second board the semiknights will never meet.
思路:
一次bfs,判断第一个K能否到达第二个K的位置,且步数为偶数
forward and 2 squares to the right, 2 squares
forward and 2 squares to the left, 2squaresbackward
and 2 to the right and 2 squares
backward and 2 to the left. Naturally, the semiknight cannotmove beyond the limits of the chessboard.
Petya put two semiknights on a standard chessboard. Petya simultaneouslymoves with both semiknights. The squares are rather large, so after some movethe semiknights can meet, that is, they can end up in the same square. Afterthe
meeting the semiknights can move on, so it is possible that they meetagain. Petya wonders if there is such sequence of moves when the semiknightsmeet. Petya considers some squares bad. That is, they do not suit for themeeting. The semiknights can move through
these squares but their meetings inthese squares don't count.
Petya prepared multiple chess boards. Help Petya find out whether thesemiknights can meet on some good square for each board.
Please see the test case analysis.
Input
The first line contains number t (1 ≤ t ≤ 50)
— the number of boards. Eachboard is described by a matrix of characters, consisting of 8 rows and 8columns. The matrix consists of characters ".", "#",
"K", representing an empty good square, a bad square and thesemiknight's position, correspondingly. It is guaranteed that matrix containsexactly 2 semiknights.
The semiknight's squares areconsidered good for the meeting. The tests are separated by empty line.
Output
For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO"
otherwise.
Sample test(s)
input
2
........
........
......#.
K..##..#
.......#
...##..#
......#.
K.......
........
........
..#.....
..#..#..
..####..
...##...
........
....K#K#
output
YES
NO
Note
Consider the first board from the sample. We will assume the rows andcolumns of the matrix to be numbered 1 through 8 from top to bottom and fromleft to right, correspondingly. The knights can meet, for example, in square (2, 7).
Thesemiknight from square (4, 1) goes tosquare (2, 3)
and thesemiknight goes from square (8, 1) tosquare (6, 3).
Thenboth semiknights go to (4,5) but thissquare is bad, so they move together to square (2, 7).
On the second board the semiknights will never meet.
思路:
一次bfs,判断第一个K能否到达第二个K的位置,且步数为偶数
代码: #define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include <queue> using namespace std; const int N = 10005; char c[15][15]; bool k[15][15]; bool p[15][15]; int tep[15][15]; int mx[] = { 2, -2, -2, 2 }; int my[] = { 2, 2, -2, -2 }; struct node { int x, y, step; }; bool check(int x, int y) { return (x >= 0 && y >= 0 && x<8 && y<8); } int main() { int t; scanf("%d", &t); while (t--) { memset(tep, -1, sizeof(tep)); int i, j; int x[2], y[2], l = 0; for (i = 0; i<8; i++) scanf("%s", c[i]); for (i = 0; i<8; i++) { for (j = 0; j<8; j++) if (c[i][j] == 'K') { x[l] = i; y[l++] = j; if (l == 2) break; } } memset(k, true, sizeof(k)); queue<node> q; node now, temp; bool flag = false; int tx, ty; now.x = x[0]; now.y = y[0]; now.step = 0; q.push(now); while (!q.empty()) { now = q.front(); q.pop(); k[now.x][now.y] = false; tep[now.x][now.y] = now.step; if (now.x == x[1] && now.y == y[1] && now.step % 2 == 0) { flag = true; break; } for (i = 0; i<4; i++) { tx = now.x + mx[i]; ty = now.y + my[i]; if (check(tx, ty) && k[tx][ty]) { temp.x = tx; temp.y = ty; temp.step = now.step + 1; q.push(temp); } } } if (flag) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- HDU 2159 FATE(二维背包)
- 背包九讲——0-1背包问题
- 04-树8. Complete Binary Search Tree (30)
- Thinkphp M 中的 where 失效问题
- 固定ListView的高度
- EALayout 第三节 依赖多个控件布局
- 关于java反射的那点事
- php实现向javascript传递数组的方法
- Cocos2d-x中,如何通过触摸来移动一个精灵
- Hadoop集群(第7期)_Eclipse开发环境设置
- ios xcode真机调试获取屏幕截屏
- XSS漏洞攻击原理与解决办法
- hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值
- 【扩展知识3】一些困难的数组
- Node.js爬虫数据抓取 -- 问题总结
- [bfs]HDU2612 Find a way
- C/C++ linux下光标定位和清屏函数(printf 实现)
- HDU 5239 DOOM 线段树
- Struts2 vs SpringMVC for Me
- 读Spring的源代码二:DispatcherServlet处理web请求的过程