UVa 11953 - Battleships
来源:互联网 发布:韩国身份证汉字 知乎 编辑:程序博客网 时间:2024/06/05 05:59
題目:打飛機,打船(中學時經常玩的):兩個人在紙上畫上船,分別盲報坐標打對方的船。
已知船的被擊中的點和未必機中的點,判斷存活的船的數量。
分析:圖論,搜索。直接利用dfs搜索即可。
說明:好久沒有刷題了╮(╯▽╰)╭。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;char grid[101][101];int dxy[4][2] = { 1, 0, 0, 1, -1, 0, 0, -1};int dfs(int x, int y, int n){if (x < 0 || x >= n || y < 0 || y >= n) {return 0;}if (grid[x][y] == '.' || grid[x][y] == '#') {return 0;}int flag = 0;if (grid[x][y] == 'x') {flag = 1;}grid[x][y] = '#';for (int i = 0; i < 4; ++ i) {if (dfs(x+dxy[i][0], y+dxy[i][1], n)) {flag = 1;}}return flag;}int main(){int T, N;while (~scanf("%d",&T)) {for (int t = 1; t <= T; ++ t) {scanf("%d",&N);for (int i = 0; i < N; ++ i) {scanf("%s",grid[i]);}int count = 0;for (int i = 0; i < N; ++ i) {for (int j = 0; j < N; ++ j) {if (grid[i][j] == 'x' || grid[i][j] == '@') {count += dfs(i, j, N);}}}printf("Case %d: %d\n",t,count);}}return 0;}
0 0
- UVa 11953 - Battleships
- codeforces100291A——Battleships
- Battleships in a Board
- Battleships in a Board
- Battleships in a Board
- Battleships in a Board
- 419. Battleships in a Board
- 419. Battleships in a Board
- 419. Battleships in a Board
- 419. Battleships in a Board
- Leetcode Battleships in a Board
- 419. Battleships in a Board
- [Medium]Battleships in a Board
- 419. Battleships in a Board
- 419. Battleships in a Board
- 419. Battleships in a Board
- 419. Battleships in a Board
- 419. Battleships in a Board
- SpringMVC基于注解的Controller详解
- Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
- 关于PX、PT、PPI、DPI、DP那些事
- chm格式文件索引不能用的解决方法
- 我的程序学习之路
- UVa 11953 - Battleships
- C++ 类的静态方法作为回调函数传给另外一个类的方法,并调用之
- centos设置静态IP
- oracle手工生成AWR报告方法记录
- vc++ 6.0转vs2010出现的问题
- AndroidStudio修改包名和项目名称
- ES6 展开运算符 Spread operator
- Poj 1456 Supermarket(2)
- CGAL编译错误