1 条题解
-
0
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; }
Pascal :
var a:array[0..51,0..51] of longint; i,n,x,y,x1,y1:longint; f:boolean; p:char; procedure work1; begin writeln('The worm ran into itself on move ',i,'.'); f:=false; end; procedure work2; begin writeln('The worm ran off the board on move ',i,'.'); f:=false; end; procedure work3; var j,k:longint; begin a[x,y]:=1; a[x1,y1]:=0; for j:=1 to 50 do for k:=1 to 50 do begin if a[j,k]<>0 then a[j,k]:=a[j,k]+1; if a[j,k]=19 then begin x1:=j; y1:=k; end; end; end; begin readln(n); while n<>0 do begin fillchar(a,sizeof(a),0); x:=25; y:=30; x1:=25; y1:=11; for i:=30 downto 11 do a[25,i]:=i-11+1; f:=true; for i:=1 to n do begin read(p); if p='N' then begin x:=x-1; if a[x,y]<>0 then begin work1; break; end; if x=0 then begin work2; break; end; work3; end; if p='S' then begin x:=x+1; if a[x,y]<>0 then begin work1; break; end; if x=51 then begin work2; break; end; work3; end; if p='W' then begin y:=y-1; if a[x,y]<>0 then begin work1; break; end; if y=0 then begin work2; break; end; work3; end; if p='E' then begin y:=y+1; if a[x,y]<>0 then begin work1; break; end; if y=51 then begin work2; break; end; work3; end; end; if f then writeln('The worm successfully made all ',i,' moves.'); readln; readln(n); end; end.
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; } }
Python :
DIRECTION = {'N': (-1, 0), 'E': (0, 1), 'S': (1, 0), 'W': (0, -1)} class Snake(): def __init__(self): self.body = [(25, x) for x in xrange(11, 31)] def touch_board(self, move_to): return not (1 <= move_to[0] <= 50 and 1 <= move_to[1] <= 50) def eat_self(self, move_to): return move_to in self.body def move(self, dir_str): del self.body[0] dir = DIRECTION[dir_str] head = self.body[-1] move_to = (head[0] + dir[0], head[1] + dir[1]) if self.eat_self(move_to): return "EAT_SELF" if self.touch_board(move_to): return "TOUCH_BOURD" else: self.body.append(move_to) return "SUCCES" while True: n = input() if n == 0: break move_str = raw_input() snake = Snake() error = False for i in xrange(len(move_str)): ret = snake.move(move_str[i]) if ret == "EAT_SELF": print "The worm ran into itself on move %d." % (i + 1) error = True break elif ret == "TOUCH_BOURD": print "The worm ran off the board on move %d." % (i + 1) error = True break if not error: print "The worm successfully made all %d moves." % (len(move_str))
- 1
信息
- ID
- 2629
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者