SCU4487 king's trouble I(深搜DFS)
题目:
Time Limit: 1000 MS Memory Limit: 131072 K
Description
Long time ago, a king occupied a vast territory. Now there is a problem that he worried that he did not know how much of the perimeter of his territory. Can you help him? For simplicity, we use a matrix to represent the territory as follows: 0 0 0 0 0 0 1 0 1 0 1 1 0 1 0 0 1 1 0 0 0 0 1 0 0Every single number in the matrix represents a piece of land, which is a 1*1 square 1 represents that this land has been occupied 0 represents notObviously the perimeter of all ocupied lands is 20
Input
The first line of the input contains a single integer t (1≤t≤5) — the number of cases. For each case The first line has two integers N and M representing the length and width of the matrix Then M lines follows to describe the matrix 1≤N,M≤400
Output
For each case output the perimeter of all ocupied lands
Sample Input
1 5 5 0 0 0 0 0 0 1 0 1 0 1 1 0 1 0 0 1 1 0 0 0 0 1 0 0
Sample Output
20
思路:
思路很简单,一种是在外层找到0然后看它的四周有几个1,另一种是找到1看他的四周有几个0,比赛的时候因为题目描述不清,我一直以为是m行,导致错了几发,最后换了一下m和n最后才A了~
代码1(1找0):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <cstring>
#include <string>
#define N 500
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int n,m;
int map[N][N];
int go[4][2]= {0,1,0,-1,1,0,-1,0};
int dfs(int x,int y)
{int sum=0;for(int i=0; i<4; i++){int xx=x+go[i][0];int yy=y+go[i][1];if(map[xx][yy]==0)sum++;}return sum;
}
int main()
{int t;scanf("%d",&t);while(t--){int i,j;mem(map,0);scanf("%d%d",&n,&m);for(int i=1; i<=n; i++)for(int j=1; j<=m; j++)scanf("%d",&map[i][j]);int sum=0;for(int i=1; i<=n; i++)for(int j=1; j<=m; j++)if(map[i][j]==1)sum+=dfs(i,j);printf("%d\n",sum);}return 0;
}
代码2(0找1,注意在外层加一圈0):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <cstring>
#include <string>
#define N 500
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int n,m;
int map[N][N];
int cnt;
int go[4][2]= {1,0,-1,0,0,1,0,-1};
int main()
{int t;scanf("%d",&t);while(t--){mem(map,0);cnt=0;scanf("%d%d",&m,&n);for(int i=0; i<=m+1; i++){map[i][0]=0;map[i][n+1]=0;}for(int i=0; i<=n+1; i++){map[0][i]=0;map[m+1][i]=0;}for(int i=1; i<=m; i++)for(int j=1; j<=n; j++)scanf("%d",&map[i][j]);for(int i=0; i<=m+1; i++){for(int j=0; j<=n+1; j++){if(map[i][j]==0){for(int k=0; k<4; k++){int x=i+go[k][0];int y=j+go[k][1];if(x>=0&&x<=m+1&&y>=0&&y<=n+1&&map[x][y]==1){cnt++;}}}}}printf("%d\n",cnt);}return 0;
}
SCU4487 king's trouble I(深搜DFS)相关推荐
- [二叉树|深搜|dfs] leetcode 404 左叶子之和
[二叉树|深搜|dfs] leetcode 404 左叶子之和 1.题目 题目链接 计算给定二叉树的所有左叶子之和. 示例: 3/ \9 20/ \15 7在这个二叉树中,有两个左叶子,分别是 9 和 ...
- 题解HDU6148 Valley Numer(数位DP+深搜DFS)
题解HDU6148 Valley Numer[数位DP+深搜DFS] 题目 解析 参考源码 题目 Description: 众所周知,度度熊非常喜欢数字. 它最近发明了一种新的数字:Valley Nu ...
- 深入递归、深搜dfs、回溯、剪纸学习。
深入递归,深搜dfs,回溯,剪枝 参考于博客 一.双管齐下解递归 "逐步生成结果"类问题之数值型 自下而上的递归(递推,数学归纳,动态规划) 解决简单情况下的问题. 推广到稍复杂情 ...
- 2412 - 和为K ---深搜dfs剪枝
**2412 - 和为K ---深搜dfs优化 **来源:东方博宜oj oj.czos.cn #include<bits/stdc++.h> using namespace std; co ...
- acwing-167. 木棒(深搜dfs+减枝)
乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过 50 个长度单位. 然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度. 请你设计一个程序,帮 ...
- 深搜DFS\广搜BFS 图初步入门
首先,不管是BFS还是DFS,由于时间和空间的局限性,它们只能解决数据量比较小的问题. 深搜,顾名思义,它从某个状态开始,不断的转移状态,直到无法转移,然后退回到上一步的状态,继续转移到其他状态,不断 ...
- 深搜(DFS),Image Perimeters
题目链接:http://poj.org/problem?id=1111 解题报告: 1.这里深搜有一点要注意,对角线上的点,如果为'.',则total不应该增加,因为这不是他的边长. #include ...
- 算法学习笔记(六) 二叉树和图遍历—深搜 DFS 与广搜 BFS
图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其 ...
- 习题9-2(免费糖果)【深搜dfs】+【记忆化搜索】
习题9-2 [UVa 10118]Free Candies(免费糖果) 题目大意: 桌上有4堆糖果,每堆有N(N<=40)颗.佳佳有个最多可以装5颗糖果的小篮子.他每次选择一堆糖果,把最顶上的一 ...
- 算法——广搜(BFS)/深搜(DFS)
在图的基本算法中,最初接触的就是图的遍历算法,根据访问节点的顺序,可分为广度优先搜索(BFS)和深度优先搜索(DFS). 广度优先搜索 广度优先搜索算法主要解决两个问题: 从节点A出发有到节点 ...
最新文章
- 2018未来科学大奖揭晓:袁隆平、马大为、林本坚等7位科学家获奖
- 无监督学习距离监督学习还有多远?Hinton组新作解读
- 一行命令搞定AD数据库备份
- MOSS 权限管理总结
- 1.1 鞅、停时和域流-随机过程的可测性(布朗运动与随机计算【习题解答】)
- ThreadLocal的第二种用法 part1
- 柯南君:看大数据时代下的IT架构(4)消息队列之RabbitMQ--案例(Helloword起航)...
- 设置 git pull 无需输入账号和密码
- 为什么有的人洗脚时要在水里滴几滴醋?
- 【转】asp.net项目在IE11下出现“__doPostBack”未定义的解决办法
- Win10 PowerToys官方免费效率小工具集
- Excel造测试大量数据
- LTE学习笔记五:LTE两种帧结构
- 爬虫学习分享Python下载mp4文件(源码)
- 139说客这个产品跟传统的微博和SNS产品的区别
- 【机器学习基础】最大边缘分类器
- 51单片机和315M无线发射模块编码与解码
- 永磁同步电机PMSM,异步电机仿真矢量控制
- 直通滤波(PassThrough 过滤器)
- HM-SpringCloud微服务系列11.1【多级缓存的意义JVM进程缓存】