SCU4487 king's trouble I(深搜DFS)
来源:互联网 发布:linux命令大全下载 编辑:程序博客网 时间:2024/05/17 22:24
题目:
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 00 1 0 1 01 1 0 1 00 1 1 0 00 0 1 0 0Every single number in the matrix represents a piece of land, which is a 1*1 square1 represents that this land has been occupied0 represents notObviously the perimeter of all ocupied lands is 20Input
The first line of the input contains a single integer t (1≤t≤5) — the number of cases.For each caseThe first line has two integers N and M representing the length and width of the matrixThen M lines follows to describe the matrix1≤N,M≤400Output
For each case output the perimeter of all ocupied landsSample Input
15 50 0 0 0 00 1 0 1 01 1 0 1 00 1 1 0 00 0 1 0 0Sample 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;}
0 0
- SCU4487 king's trouble I(深搜DFS)
- king's trouble I
- king's trouble I
- SCU 4487 king's trouble I
- SCU 4487 king's trouble I(dfs)||(0找1)
- king's trouble II
- king's trouble II
- king's trouble II SCU
- SCU4488 king's trouble II(DP)
- king's trouble II (单调栈)
- 四川省赛-E king's trouble II
- SCU 4488 king's trouble II(dp||枚举)
- SCU~4488: king's trouble II(dp)
- hdu 4337 King Arthur's Knights#DFS
- hdu4337 King Arthur's Knights(dfs回溯)
- SGU 476 Coach's Trouble(高精度,dfs,容斥)
- HDU 4337 King Arthur's Knights(暴力dfs)
- hdu 4337 King Arthur's Knights(dfs)
- struts2拦截器的使用
- CodeForces 768C Jon Snow and his Favourite Number
- HDU 猜猜谁是我 两点间距离公式
- map容器
- 猜猜谁是我
- SCU4487 king's trouble I(深搜DFS)
- [省选] [树链剖分] [BZOJ2243] [SDOI2011] 染色
- struts2学习
- 剑指offer学习笔记(Java实现)(26-30)
- 网络同步赛——猜猜谁是我
- 初始容量问题
- 算年龄
- Unity OnBecameVisible 与 OnBecameInvisible 用法
- 大话设计模式之设计模式的六大原则