ZOJ小小蠕虫-P1002

import java.util.*;
public class Main{
	static int reNum=0;
	static int reCount=0;
	static int k=0;

    public static int avaWay(int a, int [][] b,int i,int j)
	{
		int tempR=0;
		int tX=i;
		int tY=j;

		while (b[tX-1][tY]==2){tX--;}
		if (b[tX-1][tY]==1)
		{
			 tempR++;
		}
		 else
		{	
			 tX=i;
			while (b[tX+1][tY]==2){tX++;}if(b[tX+1][tY]==1){tempR++;}
		}
		tX=i;
		while (b[tX][tY-1]==2){tY--;}
		if(b[tX][tY-1]==1)
		{
			tempR++;
		}
		else 
		{
			tY=j;
		    while (b[tX][tY+1]==2){tY++;}if(b[tX][tY+1]==1){tempR++;}
		}
			tY=j;
        return tempR;
	}


    public static void modify(int a,int[][] b,int i,int j)
	{
		b[i][j]=2;
		int tX=i,tY=j;
		while (b[--tX][tY]!=0)
		{
			if(b[tX][tY]==1) b[tX][tY]=2;
		}
		tX=i;
		while (b[++tX][tY]!=0)
		{
			if(b[tX][tY]==1) b[tX][tY]=2;
		}
		tX=i;
		while (b[tX][--tY]!=0)
		{
			if(b[tX][tY]==1) b[tX][tY]=2;
		}
		tY=j;
		while (b[tX][++tY]!=0)
		{
			if(b[tX][tY]==1) b[tX][tY]=2;
		}
		tY=j;
		return;

	}
	public static void step1(int a,int[][] b)
	{
        for (int i=1 ;i<=a ;i++ )
        {
			for (int j=1 ;j<=a ;j++ )
			{
				if (b[i][j]!=0&&b[i][j]!=2)
				{
					int temp = avaWay(a,b,i,j);
					if (temp == 0)
					{
						b[i][j]=2;
						reNum++;
					}
					else if (temp == 1)
					{
                        modify(a,b,i,j);
						reNum++;
						step1(a,b);
						return;
					}
				}
			}
        }
	}
    public static int step2(int a,int[][]b)
	{
		int[] tX=new int[4];
		int[] tY=new int[4];
		boolean flag=false;
		for (int i=1 ;i<=a ;i++ )
		{
			for (int j=1;j<=a ;j++ )
			{
				if (b[i][j]==1)
				{
					tX[i-1]=1;
					tY[j-1]=1;
				}
			}
		}
		int tempX=tX[0]+tX[1]+tX[2]+tX[3];
		int tempY=tY[0]+tY[1]+tY[2]+tY[3];
		return tempX > tempY ? tempX : tempY;

	}
	public static int solve(int a , int[][] b)
	{
        step1(a,b);
        reNum+=step2(a,b);
		return reNum;

	}
	public static void main(String[] args) 
	{
		int[] result = new int[20];
		Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt())
		{
			int a = sc.nextInt();
			if(a==0) break;
			int[][] b= new int[6][6];
			sc.nextLine();
            for (int i = 1;i <= a ;i++ )
            {
               String str = sc.nextLine();
			   char[] temp = str.toCharArray();
			   for (int j=1 ;j <= a ;j++ )
			   {
				   if(temp[j-1]=='.'){b[i][j]=1;}
				   else {b[i][j]=0;}
			   }
            }

           result[reCount]=solve(a,b);
		   reNum=0;
		   reCount++;
		}
	
	   for (int i=0 ;i<reCount ;i++ )
	   {
		   System.out.println(result[i]);
	   }
	}
}

第一次提交Accept,然后是一些收获,记录下来:

1.代码中即使有注释也不能写中文,不然会报错,而且很低级==“Compilation Error”。

2.写代码的过程中,由于最开始想的时候并没有确定每个方格用什么标志位,因而后期方法确定了以后,却浪费了大量时间找逻辑错误中,其实,这些错误藏在最开始设置的标志位中!

3.好多人这么晚了没睡在提交,好厉害!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值