HLJUOJ1023(四方向搜索 + 八方向搜索)
来源:互联网 发布:vb中的对象 编辑:程序博客网 时间:2024/05/01 05:17
1023: Balloons
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 11 Solved: 5
[Submit][Status][Web Board]
Description
Input
The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N≤100), which represents the size of the matrix.
Each of the next N lines contains a string whose length is N, represents the elements of the matrix. The string only consists of 0 and 1, while 0 represents a block and 1represents balloons.
The last case is followed by a line containing one zero.
Output
For each case, print the case number (1, 2 …) and the connected block’s numbers with Saya and Kudo’s definition. Your output format should imitate the sample output. Print a blank line after each test case.
Sample Input
511001001001111111010100100
Sample Output
Case 1: 3 2
解题思路:
题意是说0代表不能走,1代表能走,问1所能构成的联通块。两个人的搜索方式不同,第一个人按四个方向搜索,第二个人按八个方向搜索。
暴力每个点,把联通的都改成0,计数器+1,。因为涉及到图的修改,所以开了一个copy_maps来保存原图。
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;const int maxn = 1111;int maps[maxn][maxn];int copy_maps[maxn][maxn];int n;string str;int vis[maxn][maxn];int d1[4][2] = { -1 , 0 , 0 , 1 , 1 , 0 , 0 , -1 };int d2[8][2] = { -1 , 0 , -1 , 1 , 0 , 1 , 1 ,1 , 1 , 0 , 1 , -1 , 0 , -1 , -1 , -1};void dfs1(int x , int y){ vis[x][y] = 1; for(int i = 0 ; i < 4 ; i ++) { int xx = x + d1[i][0]; int yy = y + d1[i][1]; if(xx >= 0 && xx < n && yy >= 0 && yy < n && !vis[xx][yy] && maps[xx][yy] == 1) { maps[xx][yy] = 0; dfs1(xx , yy); } }}void dfs2(int x , int y){ vis[x][y] = 1; for(int i = 0 ; i < 8 ; i ++) { int xx = x + d2[i][0]; int yy = y + d2[i][1]; if(xx >= 0 && xx < n && yy >= 0 && yy < n && !vis[xx][yy] && copy_maps[xx][yy] == 1) { copy_maps[xx][yy] = 0; dfs2(xx , yy); } }}int main(){ #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int cas = 1; while(~scanf("%d",&n)) { if(n == 0) break; for(int i = 0 ; i < n ; i ++) { cin >> str; for(int j = 0 ; j < n ; j ++) { maps[i][j] = str[j] - '0'; copy_maps[i][j] = maps[i][j]; } } int cnt1 = 0 , cnt2 = 0; memset(vis , 0 , sizeof(vis)); for(int i = 0 ; i < n ; i ++) { for(int j = 0 ; j < n ; j ++) { if(maps[i][j] == 1) { dfs1(i , j); cnt1 ++; } } } memset(vis , 0 , sizeof(vis)); for(int i = 0 ; i < n ; i ++) { for(int j = 0 ; j < n ; j ++) { if(copy_maps[i][j] == 1) { dfs2(i , j); cnt2 ++; } } } printf("Case %d: %d %d\n\n" , cas ++ , cnt1 , cnt2); }}
0 0
- HLJUOJ1023(四方向搜索 + 八方向搜索)
- 2016暑期集训---搜索(BFS 八方向马步问题)
- A*搜索四方向寻路 C++代码实现
- CodeForces 540C--自带标记的四方向搜索
- 迷宫问题(八方向)
- UVA10010(八方向暴力枚举)
- 迷宫寻路(八方向)
- 八方向行走脚本
- NGUI八方向
- dfs八向搜索
- HDU1035 四方向dfs
- 八方向迷宫 C++版
- BFS - 八方向迷宫问题
- 八方向移动限制想法
- A*算法学习笔记-(八方向,非最优解)
- UVA 10010- Where's Waldorf?(八方向寻找字符串)
- cocosCreator 键盘四方向移动
- (12.18)无向图的深度搜索(邻接矩阵)
- linux系统下挂载命令详解 mount
- 第三章顺序栈
- 第十三周项目一 数组大折腾(1)
- OAuth 2.0 概述
- 最小路径覆盖问题(最小路径覆盖)
- HLJUOJ1023(四方向搜索 + 八方向搜索)
- 完美数
- Nginx配置文件nginx.conf中文详解(总结)
- 基于ALS算法的简易在线推荐系统
- qt plugin
- ios中对于tabBar选中图片设置的问题
- 第13周项目4数组的排序
- 用C语言编写一个随机点名系统
- KeychainItemWrapper的使用