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 20

Input

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≤400

Output

For each case output the perimeter of all ocupied lands

Sample Input

15 50 0 0 0 00 1 0 1 01 1 0 1 00 1 1 0 00 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;}


0 0
原创粉丝点击