1 条题解
-
0
C :
#include <stdio.h> int main() { int arr[3][3]; int i, j, temp; int zero[2]; char c; for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); if(arr[i][j] == 0) { zero[0] = i; zero[1] = j; } } } i = zero[0]; j = zero[1]; scanf("%c", &c); while(c != 'E') { switch(c) { case 'U': i--; break; case 'D': i++; break; case 'L': j--; break; case 'R': j++; break; } temp = arr[i][j]; arr[i][j] = 0; arr[zero[0]][zero[1]] = temp; zero[0] = i; zero[1] = j; scanf("%c", &c); } for(i = 0; i < 3; i++) { printf("%d %d %d\n", arr[i][0], arr[i][1], arr[i][2]); } return 0; }
C++ :
#include<iostream> #include<string> using namespace std; void swap(int *a,int *b){ int *t =a; a = b; b = t; } int main(){ int xi,xj,x; int a[3][3]; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ cin>>x; a[i][j] = x; if(x==0){ xi=i; xj=j; } } } char ch; // while(cin>>ch && *ch!='E') // cout<<ch; while(cin>>ch&&ch!='E'){ if(ch=='U'){ swap(a[xi][xj],a[xi-1][xj]); xi = xi-1; } else if(ch=='D'){ swap(a[xi][xj],a[xi+1][xj]); xi = xi+1; } else if(ch=='L'){ swap(a[xi][xj],a[xi][xj-1]); xj = xj-1; } else if(ch=='R'){ swap(a[xi][xj],a[xi][xj+1]); xj = xj+1; } } for(int m=0;m<3;m++){ for(int n=0;n<3;n++){ if(n==0) cout<<a[m][n]; else cout<<" "<<a[m][n]; } cout<<endl; } return 0; } /* 3 4 0 7 8 5 6 1 2 D D L U L R R E */
Java :
import java.util.Scanner; public class Main { public static int[] a = new int[9]; public static void main(String[] args) { int t = 0, n, i; String s; Scanner in = new Scanner(System.in); for (i = 0; i < 9; i++) { n = in.nextInt(); if (n == 0) { t = i; } a[i] = n; } while (in.hasNext()) { s = in.next(); if (s.equals("U")) { s(t, t - 3); t = t - 3; } if (s.equals("D")) { s(t, t + 3); t = t + 3; } if (s.equals("L")) { s(t, t - 1); t = t - 1; } if (s.equals("R")) { s(t, t + 1); t = t + 1; } if (s.equals("E")) { break; } } for (i = 0; i < 9; i++) { if ((i + 1) % 3 != 0) { System.out.print(a[i] + " "); } else { System.out.println(a[i]); } } } public static void s(int x, int y) { int t = a[x]; a[x] = a[y]; a[y] = t; } }
- 1
信息
- ID
- 221
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者