zoj 2412 搜索

来源:互联网 发布:网络搬砖是什么意思 编辑:程序博客网 时间:2024/06/01 08:16

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1412

我的代码太长了,太丑了,我的优化方法有问题

一下是我的代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;


const int sizen=1000;
bool Map[sizen][sizen];
int Mat[sizen][sizen];
int m,n;
int d[4][2]={1,0,-1,0,0,1,0,-1};


void dfs(int x,int y)
{
    int i;
    int mx,my;
    for(i=0;i<4;i++)
    {
        mx=x+d[i][0];
        my=y+d[i][1];
        if(Map[mx][my])
        {
            Map[mx][my]=false;
            dfs(mx,my);
        }
    }
}


int main()
{
    int cnt;
    int i,j;
    char c;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        cnt=0;
        if(m<0||n<0)
            break;
        memset(Map,false,sizeof(Map));
        memset(Mat,0,sizeof(Mat));
        for(i=1;i<=m;i++)
            for(j=1;j<=n;j++)
            {
                cin>>c;
                Map[i*2][j*2]=true;
                if(c=='A')
                {
                    Mat[i*2][j*2-1]++;
                    if(Mat[i*2][j*2-1]==2)
                        Map[i*2][j*2-1]=true;
                    Mat[i*2-1][j*2]++;
                    if(Mat[i*2-1][j*2]==2)
                        Map[i*2-1][j*2]=true;
                }
                if(c=='B')
                {
                    Mat[i*2-1][j*2]++;
                    if(Mat[i*2-1][j*2]==2)
                        Map[i*2-1][j*2]=true;
                    Mat[i*2][j*2+1]++;
                    if(Mat[i*2][j*2+1]==2)
                        Map[i*2][j*2+1]=true;
                }
                if(c=='C')
                {
                    Mat[i*2][j*2-1]++;
                    if(Mat[i*2][j*2-1]==2)
                        Map[i*2][j*2-1]=true;
                    Mat[i*2+1][j*2]++;
                    if(Mat[i*2+1][j*2]==2)
                        Map[i*2+1][j*2]=true;
                }
                if(c=='D')
                {
                    Mat[i*2+1][j*2]++;
                    if(Mat[i*2+1][j*2]==2)
                        Map[i*2+1][j*2]=true;
                    Mat[i*2][j*2+1]++;
                    if(Mat[i*2][j*2+1]==2)
                        Map[i*2][j*2+1]=true;
                }
                if(c=='E')
                {
                    Mat[i*2-1][j*2]++;
                    if(Mat[i*2-1][j*2]==2)
                        Map[i*2-1][j*2]=true;
                    Mat[i*2+1][j*2]++;
                    if(Mat[i*2+1][j*2]==2)
                        Map[i*2+1][j*2]=true;
                }
                if(c=='F')
                {
                    Mat[i*2][j*2-1]++;
                    if(Mat[i*2][j*2-1]==2)
                        Map[i*2][j*2-1]=true;
                    Mat[i*2][j*2+1]++;
                    if(Mat[i*2][j*2+1]==2)
                        Map[i*2][j*2+1]=true;
                }
                if(c=='G')
                {
                    Mat[i*2-1][j*2]++;
                    if(Mat[i*2-1][j*2]==2)
                        Map[i*2-1][j*2]=true;
                    Mat[i*2][j*2+1]++;
                    if(Mat[i*2][j*2+1]==2)
                        Map[i*2][j*2+1]=true;
                    Mat[i*2][j*2-1]++;
                    if(Mat[i*2][j*2-1]==2)
                        Map[i*2][j*2-1]=true;
                }
                if(c=='H')
                {
                    Mat[i*2-1][j*2]++;
                    if(Mat[i*2-1][j*2]==2)
                        Map[i*2-1][j*2]=true;
                    Mat[i*2+1][j*2]++;
                    if(Mat[i*2+1][j*2]==2)
                        Map[i*2+1][j*2]=true;
                    Mat[i*2][j*2-1]++;
                    if(Mat[i*2][j*2-1]==2)
                        Map[i*2][j*2-1]=true;
                }
                if(c=='I')
                {
                    Mat[i*2][j*2-1]++;
                    if(Mat[i*2][j*2-1]==2)
                        Map[i*2][j*2-1]=true;
                    Mat[i*2][j*2+1]++;
                    if(Mat[i*2][j*2+1]==2)
                        Map[i*2][j*2+1]=true;
                    Mat[i*2+1][j*2]++;
                    if(Mat[i*2+1][j*2]==2)
                        Map[i*2+1][j*2]=true;
                }
                if(c=='J')
                {
                    Mat[i*2-1][j*2]++;
                    if(Mat[i*2-1][j*2]==2)
                        Map[i*2-1][j*2]=true;
                    Mat[i*2+1][j*2]++;
                    if(Mat[i*2+1][j*2]==2)
                        Map[i*2+1][j*2]=true;
                    Mat[i*2][j*2+1]++;
                    if(Mat[i*2][j*2+1]==2)
                        Map[i*2][j*2+1]=true;
                }
                if(c=='K')
                {
                    Mat[i*2-1][j*2]++;
                    if(Mat[i*2-1][j*2]==2)
                        Map[i*2-1][j*2]=true;
                    Mat[i*2+1][j*2]++;
                    if(Mat[i*2+1][j*2]==2)
                        Map[i*2+1][j*2]=true;
                    Mat[i*2][j*2+1]++;
                    if(Mat[i*2][j*2+1]==2)
                        Map[i*2][j*2+1]=true;
                    Mat[i*2][j*2-1]++;
                    if(Mat[i*2][j*2-1]==2)
                        Map[i*2][j*2-1]=true;
                }
            }
            for(i=1;i<=2*m+1;i++)
                for(j=1;j<=2*n+1;j++)
                    if(Map[i][j])
                    {
                        Map[i][j]=false;
                        dfs(i,j);
                        cnt++;
                    }
            printf("%d\n",cnt);
    }
    return 0;
}

一下是我认为很好的代码

#include<stdio.h>
#include<string.h>
 
#define I (i*3)
#define J (j*3)
char str[51][51];
int map[159][159];
int n, m;
int dir[4][2] = 
{
    {0, 1}, {0, -1}, {-1, 0}, {1, 0}
};
 
void dfs(int i, int j)
{
    map[i][j] = 0;
    for(int k = 0; k < 4; k++) {
        int x = i + dir[k][0];
        int y = j + dir[k][1];
        if(x >= 0 && x < 3*n && y >= 0 && y < 3*m && map[x][y]) {
            dfs(x, y);
        }
    }
}
 
int main()
{
    while(scanf("%d %d%*c", &n, &m) != EOF) {
        if(n < 0 || m < 0) break;
        memset(map, 0, sizeof(map));
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                scanf("%c", &str[i][j]);
                if( str[i][j] == 'A' ) {
                    map[I][J+1] = map[I+1][J] = map[I+1][J+1] = 1;
                }
                if( str[i][j] == 'B' ) {
                    map[I+1][J+1] = map[I+1][J+2] = map[I][J+1] = 1;
                }
                if( str[i][j] == 'C' ) {
                    map[I+2][J+1] = map[I+1][J] = map[I+1][J+1] = 1;
                }
                if( str[i][j] == 'D' ) {
                    map[I+2][J+1] = map[I+1][J+2] = map[I+1][J+1] = 1;
                }
                if( str[i][j] == 'E' ) {
                    map[I][J+1] = map[I+2][J+1] = map[I+1][J+1] = 1;
                }
                if( str[i][j] == 'F' ) {
                    map[I+1][J+2] = map[I+1][J] = map[I+1][J+1] = 1;
                }
                if( str[i][j] == 'G' ) {
                    map[I+1][J+2] = map[I+1][J] = map[I+1][J+1] = map[I][J+1] = 1;
                }
                if( str[i][j] == 'H' ) {
                    map[I][J+1] = map[I+2][J+1] = map[I+1][J+1] = map[I+1][J] = 1;
                }
                if( str[i][j] == 'I' ) {
                    map[I+1][J+2] = map[I+1][J] = map[I+1][J+1] = map[I+2][J+1] = 1;
                }
                if( str[i][j] == 'J' ) {
                    map[I][J+1] = map[I+2][J+1] = map[I+1][J+1] = map[I+1][J+2] = 1;
                }
                if( str[i][j] == 'K' ) {
                    map[I][J+1] = map[I+2][J+1] = map[I+1][J+1] = map[I+1][J+2] = map[I+1][J] = 1;
                }
            }
            scanf("%*c");
        }
         
        int ans = 0;
        for(int i = 0; i < 3*n; i++) {
            for(int j = 0; j < 3*m; j++) {
                if(map[i][j]) {
                    dfs(i, j);
                    ans++;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
他的宏定义

#define I (i*3)
#define J (j*3)

感觉特牛,我需要学习一下

还有的是他乘以上的思路太吊了



0 0
原创粉丝点击