Codeforces Round #361 (Div. 2)

A.   Codeforces 689A Mike and Cellphone

   

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <map>
 4 #include <vector>
 5 #include <functional>
 6 #include <string>
 7 #include <cstring>
 8 #include <queue>
 9 #include <stack>
10 #include <set>
11 #include <cmath>
12 #include <cstdio>
13 using namespace std;
14 #define IOS ios_base::sync_with_stdio(false)
15 #define TIE std::cin.tie(0)
16 typedef long long LL;
17 const double PI=4.0*atan(1.0);
18 
19 int n;
20 bool board[6][6];
21 char ch;
22 int a[15];
23 int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
24 bool check()
25 {
26     int nx,ny;
27     for(int i=0;i<4;i++){
28         bool ov=false;
29         for(int j=1;j<=n;j++){
30             if(a[j]==0){
31                 nx=4+dx[i]; ny=2+dy[i];
32             }else{
33                 nx=(a[j]-1)/3+1+dx[i];
34                 ny=(a[j]-1)%3+1+dy[i];
35             }
36             if(!board[nx][ny]) ov=true;
37         }
38         if(!ov) return false;
39     }
40     return true;
41 }
42 int main()
43 {
44     while(~scanf("%d",&n)){
45         memset(board,0,sizeof(board));
46         getchar();
47         for(int i=1;i<=n;i++){
48             scanf("%c",&ch);
49             a[i]=ch-'0';
50         }
51         getchar();
52         for(int i=1;i<=9;i++)
53             board[(i-1)/3+1][(i-1)%3+1]=true;
54         board[4][2]=true;
55         printf("%s\n",check()?"YES":"NO");
56     }
57 }
View Code

    自己的代码很冗长,以下forever97大神的代码,十分简短。

 1 #include <cstdio>
 2 using namespace std;
 3 char s[10000];
 4 int n,U,D,L,R;
 5 int main(){
 6     scanf("%d %s",&n,s);
 7     for(int i=0;i<n;i++){
 8         if(s[i]=='0')D=L=R=1;
 9         if(s[i]=='1'||s[i]=='4'||s[i]=='7')L=1;
10         if(s[i]=='3'||s[i]=='6'||s[i]=='9')R=1;
11         if(s[i]=='1'||s[i]=='2'||s[i]=='3')U=1;
12         if(s[i]=='7'||s[i]=='9')D=1;
13     }if(L&&R&&U&&D)puts("YES");
14     else puts("NO");
15     return 0;
16 }

 

posted @ 2016-07-29 15:31  Cumulonimbus  阅读(159)  评论(0编辑  收藏  举报