C# CodinGame记录(1)--THERE IS NO SPOON - EPISODE 1

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

/**
 * Don't let the machines win. You are humanity's last hope...
 **/
class Player
{
    static void Main(string[] args)
    {
        int width = int.Parse(Console.ReadLine()); // the number of cells on the X axis
        int height = int.Parse(Console.ReadLine()); // the number of cells on the Y axis
        string[,] P = new string[height,width];
        int x1=0,x2=0,x3=0;
        int y1=0,y2=0,y3=0;
        for (int i = 0; i < height; i++)
        {
            string line = Console.ReadLine(); // width characters, each either 0 or .
            for(int j =0;j<width;j++)
            {
                P[i,j]=line.Substring(j,1);
            }
        }

        // Write an action using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");
        for(int i =0;i<height;i++)
        {
            for(int j =0;j<width;j++)
            {
                if(P[i,j]=="0")//x1,y1
                {
                    x1=j;
                    y1=i;
                }
                else
                {
                    x1=-1;
                    y1=-1;
                }
                int k =0;//x2,y2
                x2=j+1;
                y2=i;                
                do
                {
                    if(j+k==width-1)
                    {
                        x2=-1;
                        y2=-1;
                        break;
                    }
                    k++;
                    x2=j+k;
                    y2=i;  
                }while(P[i,j+k] != "0");    
                int n=0;//x3,y3
                x3=j;
                y3=i+1;
                do
                {
                    if(i+n == height-1)
                    {
                        x3=-1;
                        y3=-1; 
                        break;      
                    }
                    n++;    
                    x3=j;
                    y3=i+n;
                }while(P[i+n,j] !="0");                
                if(x1 != -1)//x1min =0
                {   
                    Console.WriteLine("{0} {1} {2} {3} {4} {5}",x1,y1,x2,y2,x3,y3);
                }
                
            }
        }
        // Three coordinates: a node, its right neighbor, its bottom neighbor
        
    }
}

整了两天,但这个代码嵌套太多,极其辣鸡

最早嵌套的太多,输出时间太长,改用while,但是会导致数组index溢出

后来改用do...while循环就解决了,虽然还是很繁琐


看了别人的也不太看得懂,加油吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值