1 条题解

  • 0
    @ 2024-12-24 9:49:41

    C :

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	int n,i,j,x[21],y[21],flag;
    	char str[100];
    	while(1)
    	{
    		scanf("%d",&n);
    		if(n==0) break;
    		for(i=1;i<=20;i++)
    		{
    			x[i]=25;
    			y[i]=31-i;
    		}
    		scanf("%s",&str);
    		for(flag=0,i=0;i<n;i++)
    		{
    			for(j=20;j>1;j--) {x[j]=x[j-1];y[j]=y[j-1];}
    			if(str[i]=='E') y[1]+=1;
    			if(str[i]=='S') x[1]+=1;
    			if(str[i]=='W') y[1]-=1;
    			if(str[i]=='N') x[1]-=1;
    			if(x[1]<1||x[1]>50||y[1]<1||y[1]>50) flag=1;
    			for(j=20;j>1;j--) 
    				if(x[j]==x[1]&&y[j]==y[1])
    				{
    					flag=2;
    					break;
    				}
    			if(flag==1) 
    			{
    				printf("The worm ran off the board on move %d.\n",i+1);
    				break;
    			}
    			if(flag==2)
    			{
    				printf("The worm ran into itself on move %d.\n",i+1);
    				break;
    			}
    		}
    		if(flag==0) printf("The worm successfully made all %d moves.\n",n);
    	}
    	return 0;
    }
    
    

    C++ :

    #include <cstdio>
    #include <vector>
    #include <deque>
    #include <algorithm>
    using namespace std;
    
    const int ix[4] = {-1, 0, 1, 0};
    const int iy[4] = {0, 1, 0, -1};
    int dir[300];
    
    void solve(char *s, int m) {
        static int n = 50;
        vector < vector < bool > > data(n, vector < bool >(n));
        fill(data[24].begin() + 10, data[24].begin() + 30, true);
        deque < int > q;
        for (int y = 10; y < 30; ++y)
            q.push_back(2400 + y);
        for (int i = 1; i <= m; ++i) {
            int k = dir[ s[i - 1] ];
            int x = q.back() / 100, y = q.back() % 100;
            int nx = x + ix[k], ny = y + iy[k];
            if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
                printf("The worm ran off the board on move %d.\n", i);
                return;
            }
            data[q.front() / 100][q.front() % 100] = false;
            if (data[nx][ny]) {
                printf("The worm ran into itself on move %d.\n", i);
                return;
            }
            data[nx][ny] = true;
            q.pop_front();
            q.push_back(nx * 100 + ny);
        }
        printf("The worm successfully made all %d moves.\n", m);
    }
    
    int main() {
        dir['N'] = 0;
        dir['E'] = 1;
        dir['S'] = 2;
        dir['W'] = 3;
        char str[205];
        int n;
        while (scanf("%d", &n) && n > 0) {
            scanf("%s", str);
            solve(str, n);
        }
        return 0;
    }
    

    Java :

    import java.util.*;
    public class Main {
    	static int[][] map=null;
    	static int n=0;
    	static String str=null;
    	static char c;
    	static int headx;
    	static int heady;
    	static int tailx;
    	static int taily;
    	static int step;
    	public static void main(String[] args) {
    		Scanner in=new Scanner(System.in);
    		
    		while(in.hasNext()){
    			n=Integer.parseInt(in.nextLine());
    			if(n==0){
    				break;
    			}
    			str=in.nextLine();
    			map=new int[50][50];
    			step=0;
    			//初始化地图
    			for(int i=10;i<=29;i++){
    				map[24][i]=1;
    			}
    			headx=24;
    		    heady=29;
    			tailx=24;
    			taily=10;
    			//
    			int num=move();
    			 if (num == 1)
                     System.out.println("The worm ran into itself on move "+step+".");
    			 	else if (num == 2)
    			 			System.out.println("The worm ran off the board on move "+step+".");
    			 			else
    			 				System.out.println("The worm successfully made all "+step+" moves.");
    			
    			}
    			
    			
    		}
    		
    	
    	public static int move(){
    		
    		for(int i=0;i<n;i++){
    			 step=i+1;
    			c=str.charAt(i);
    			//
    			switch(c){
    			case 'W':
    				heady--;
    				break;
    			case 'E':
    				heady++;
    				break;
    			case 'S':
    				headx--;
    				break;
    			case 'N':
    				headx++;
    				break;
    			}
               map[tailx][taily]=0;
    			if (headx >= 50 || headx < 0 || heady >=50 || heady < 0 || tailx>=50 || tailx < 0 || taily >=50 || taily < 0)
                return 2;
    			if (map[headx][heady]==1)
                    return 1;
    			map[headx][heady]=1;
    			
    			 if (step <= 19)
                     taily++;
             else {
                     switch (str.charAt(step - 20)) {
                     case 'E':
                             taily++;
                             break;
                     case 'W':
                             taily--;
                             break;
                     case 'N':
                             tailx--;
                             break;
                     case 'S':
                             tailx++;
                             break;
                     }
             }
    			
    	}
    		return 3;
    }
    }
    
    • 1

    信息

    ID
    1012
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者