• NKU 专题一 题解


    A - Flip Game

    总的情况数只有2^16次方种,显然直接bfs就可以了

     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 using namespace std;
     5 int W,B,start;
     6 bool have[1000000];
     7 struct plot{
     8     int n,step;
     9 };
    10 void input(int &n){
    11     n=0;
    12     char c;
    13     for(int i=0;i<16;i++){
    14         cin>>c;
    15         if(c=='b')n=n|(1<<i);
    16     }
    17 }
    18 void turn(int &n,int k){
    19     n=n^(1<<k);
    20     if(k%4)n=n^(1<<(k-1));
    21     if(k%4!=3)n=n^(1<<(k+1));
    22     if(k/4)n=n^(1<<(k-4));
    23     if(k/4<3)n=n^(1<<(k+4));
    24 }
    25 bool bfs(){
    26     queue<plot> q;
    27     plot p;
    28     p.n=start;p.step=0;
    29     q.push(p);
    30     memset(have,0,sizeof(have));
    31     while(!q.empty()){
    32         int cur=q.front().n,step=q.front().step;
    33         q.pop();
    34         if(have[cur])continue;
    35         if(cur==W||cur==B){
    36             cout<<step<<endl;
    37             return true;
    38         }
    39         for(int i=0;i<16;i++){
    40             p.n=cur;
    41             turn(p.n,i);
    42             p.step=step+1;
    43             if(!have[p.n])
    44                 q.push(p);
    45         }
    46         have[cur]=true;
    47     }
    48     return false;
    49 }
    50 int main(){
    51     W=0;B=0;
    52     for(int i=0;i<16;i++){
    53         B=B|(1<<i);
    54     }
    55     input(start);
    56     if(!bfs())cout<<"Impossible"<<endl;
    57     return 0;
    58 }
    View Code

    B - The Pilots Brothers' refrigerator

     直接搜应该是可以的,但是不知道为什么一直T..

    注意到如果要改变(i,j),只需要把这个十字上的全部点一遍,结果就只有(i,j)改变.改变了偶数次的相当于没有改变.

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 int have[4][4];
     5 int main(){
     6     for(int i=0;i<4;i++)
     7         for(int j=0;j<4;j++)have[i][j]=0;
     8     int a[4][4];
     9     char c;
    10     for(int i=0;i<4;i++)
    11     for(int j=0;j<4;j++){
    12         cin>>c;
    13         if(c=='+')a[i][j]=0;
    14         else a[i][j]=1;
    15     }
    16     for(int i=0;i<4;i++){
    17         for(int j=0;j<4;j++){
    18             if(a[i][j]==0){
    19                 for(int k=0;k<4;k++){
    20                     have[i][k]+=1;
    21                     have[k][j]+=1;
    22                 }
    23                 have[i][j]-=1;
    24             }
    25         }
    26     }
    27     int count=0;
    28     for(int i=0;i<4;i++){
    29         for(int j=0;j<4;j++)
    30             if(have[i][j]%2)
    31                 count++;
    32     }
    33     cout<<count<<endl;
    34     for(int i=0;i<4;i++){
    35         for(int j=0;j<4;j++)
    36             if(have[i][j]%2)
    37                 cout<<i+1<<' '<<j+1<<endl;
    38     }
    39     return 0;
    40 }
    View Code

    C - Radar Installation

     勾股定理先算一下每个小岛对应x轴上的一个区间,雷达放在这个区间内都可以扫到这个小岛,然后贪心一下就行了,按区间左端点排序,每次都放在未取点的区间左端点.

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 using namespace std;
     6 const int maxn=1005;
     7 int n,d,x[maxn],y[maxn],k[maxn],Max[maxn];
     8 bool have[maxn];
     9 double l[maxn],r[maxn];
    10 bool Cmpk(int i,int j){
    11     if(l[i]==l[j])return r[i]>r[j];
    12     return l[i]>l[j];
    13 }
    14 bool Cmpm(int i,int j){
    15     return r[i]>r[j];
    16 }
    17 void turn(){
    18     for(int i=0;i<n;i++){
    19         l[i]=x[i]-sqrt((double)d*d-y[i]*y[i]);
    20         r[i]=x[i]+sqrt((double)d*d-y[i]*y[i]);
    21     }
    22     sort(k,k+n,Cmpk);
    23     sort(Max,Max+n,Cmpm);
    24 }
    25 int solve(){
    26     turn();
    27     int j=0,count=0;
    28     for(int i=0;i<n;i++){
    29         if(have[k[i]])continue;
    30         have[k[i]]=true;
    31         count++;
    32         while(j<n&&l[k[i]]<=r[Max[j]]){
    33             have[Max[j]]=true;j++;
    34         }
    35     }
    36     return count;
    37 }
    38 int main(){
    39     int kase=0;
    40     while(cin>>n>>d&&n){
    41         memset(have,0,sizeof(have));
    42         bool ans=true;
    43         for(int i=0;i<n;i++){
    44             cin>>x[i]>>y[i];
    45             k[i]=i;
    46             Max[i]=i;
    47             if(y[i]>d){
    48                 ans=false;
    49             }
    50         }
    51         if(ans)cout<<"Case "<<++kase<<": "<<solve()<<endl;
    52         else cout<<"Case "<<++kase<<": "<<-1<<endl;
    53     }
    54     return 0;
    55 }
    View Code

    D - Power of Cryptography

    显然要用高精度..然后二分一下答案..一开始一直T,应该是卡在二分的死循环上,如果一定有解,肯定可以跳出循环的..题目应该是有问题的,不一定有恰好等的k,而是k^n<=p,取最大的k,加上这个就过了...

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<string>
      4 #include<vector>
      5 #include<iomanip>
      6 #include<cmath>
      7 using namespace std;
      8 const int BASE=1000000000,WIDTH=9;
      9 typedef long long ll;
     10 struct BigInt{
     11     bool neg;
     12     vector<int> s;
     13     BigInt operator =(string &str){
     14         s.clear();
     15         neg=false;
     16         if(str[0]=='-'){
     17             neg=true;
     18             str[0]='0';
     19         }
     20         int x,len=(str.length()-1)/WIDTH+1;
     21         for(int i=0;i<len;i++){
     22             int end=str.length()-i*WIDTH;
     23             int start=max(0,end-WIDTH);
     24             sscanf(str.substr(start,end-start).c_str(),"%d",&x);
     25             s.push_back(x);
     26         }
     27         return *this;
     28     }
     29     BigInt operator =(int n){
     30         s.clear();
     31         if(n<0){
     32             neg=true;
     33             s.push_back(-n);
     34         }else{
     35             neg=false;
     36             s.push_back(n);
     37         }
     38         return *this;
     39     }
     40     void print(){
     41         if(neg)cout<<'-';
     42         cout<<s.back();
     43         for(int i=s.size()-2;i>=0;i--){
     44             cout<<setw(8)<<setfill('0')<<s[i];
     45         }
     46         cout<<endl;
     47     }
     48 };
     49 bool operator ==(const BigInt &a,const BigInt &b){
     50     if(a.neg!=b.neg)return false;
     51     if(a.s.size()==b.s.size()){
     52         for(int i=0;i<a.s.size();i++)
     53             if(a.s[i]!=b.s[i])return false;
     54         return true;
     55     }
     56     return false;
     57 }
     58 bool operator<(BigInt &a,BigInt &b){
     59     if(a.neg&&!b.neg)return true;
     60     if(!a.neg&&b.neg)return false;
     61     if(a.neg&&b.neg){
     62         a.neg=b.neg=false;
     63         bool ans=a<b||a==b;
     64         a.neg=b.neg=true;
     65         return !ans;
     66     }
     67     if(a.s.size()<b.s.size())return true;
     68     else if(a.s.size()>b.s.size())return false;
     69     for(int i=a.s.size()-1;i>=0;i--){
     70         if(a.s[i]==b.s[i])continue;
     71         if(a.s[i]<b.s[i])return true;
     72         return false;
     73     }
     74     return false;
     75 }
     76 bool operator >(BigInt &a,BigInt &b){return !(a==b&&a<b);}
     77 bool operator <=(BigInt &a,BigInt &b){return (a==b||a<b);}
     78 bool operator >=(BigInt &a,BigInt &b){return !(a<b);}
     79 bool operator !=(BigInt &a,BigInt &b){return !(a==b);}
     80 BigInt operator -(BigInt &a,BigInt &b);
     81 BigInt operator +(BigInt &a,BigInt &b){
     82     BigInt ans;
     83     if(a.neg&&!b.neg){
     84         a.neg=false;
     85         ans=b-a;
     86         a.neg=true;
     87         return ans;
     88     }
     89     if(!a.neg&&b.neg){
     90         b.neg=false;
     91         ans=a-b;
     92         b.neg=true;
     93         return ans;
     94     }
     95     ans.neg=a.neg;
     96     int k=0,i=0;
     97     while(i<a.s.size()||i<b.s.size()){
     98         if(i<a.s.size())k+=a.s[i];
     99         if(i<b.s.size())k+=b.s[i];
    100         ans.s.push_back(k%BASE);
    101         k/=BASE;
    102         i++;
    103     }
    104     if(k)ans.s.push_back(k);
    105     return ans;
    106 }
    107 BigInt operator -(BigInt &a,BigInt &b){
    108     BigInt ans;
    109     if(!a.neg&&b.neg){
    110         b.neg=false;
    111         ans=a+b;
    112         b.neg=true;
    113         return ans;
    114     }
    115     if(a.neg&&!b.neg){
    116         a.neg=false;
    117         ans=a+b;
    118         a.neg=true;
    119         ans.neg=true;
    120         return ans;
    121     }
    122     if(a.neg&&b.neg&&a>b){
    123         b.neg=a.neg=false;
    124         ans=b-a;
    125         b.neg=a.neg=true;
    126         return ans;
    127     }
    128     if(!a.neg&&!b.neg&&a<b){
    129         ans=b-a;
    130         ans.neg=true;
    131         return ans;
    132     }
    133     ans.neg=false;
    134     int k=0,i=0;
    135     while(i<a.s.size()||i<b.s.size()){
    136         if(i<a.s.size())k+=a.s[i];
    137         if(i<b.s.size())k-=b.s[i];
    138         if(k<0){
    139             ans.s.push_back(k+BASE);
    140             k=-1;
    141         }else{
    142             ans.s.push_back(k);
    143             k=0;
    144         }
    145         i++;
    146     }
    147     while(ans.s.size()>1&&i>=0&&ans.s[--i]==0)ans.s.pop_back();
    148     return ans;
    149 }
    150 BigInt operator *(BigInt &a,BigInt &b){
    151     if(a.s.size()<b.s.size())return b*a;
    152     BigInt ans;
    153     ll k=0;
    154     for(int l=0;l<a.s.size()+b.s.size()-1;l++){
    155         for(int i=min(l,int(a.s.size()-1));i>=0&&l-i<b.s.size();i--){
    156             k+=(ll)a.s[i]*b.s[l-i];
    157         }
    158         ans.s.push_back(k%BASE);
    159         k/=BASE;
    160     }
    161     if(k)ans.s.push_back(k);
    162     ans.neg=a.neg^b.neg;
    163     return ans;
    164 }
    165 BigInt operator *(BigInt &a,int b){
    166     BigInt ans;
    167     ll k=0;
    168     for(int l=0;l<a.s.size();l++){
    169         k+=a.s[l]*b;
    170         ans.s.push_back(k%BASE);
    171         k/=BASE;
    172     }
    173     if(k)ans.s.push_back(k);
    174     if((b<0&&a.neg)||(b>0&&!a.neg))ans.neg=false;
    175     else ans.neg=true;
    176     return ans;
    177 }
    178 inline void devide_2(BigInt &a){
    179     int k=0;
    180    // a.print();
    181     for(int i=a.s.size()-1;i>=0;i--){
    182         int k0=k;
    183         k=a.s[i]%2==0?0:BASE/2;
    184         //cout<<a.s[i]<<endl;
    185         a.s[i]=k0+a.s[i]/2;
    186         //cout<<a.s[i]<<endl;
    187     }
    188     if(a.s.back()==0)a.s.pop_back();
    189 }
    190 inline BigInt devide(BigInt &a,BigInt &b,BigInt &r){
    191     BigInt L,R,m,t;
    192     L=1;R=a;
    193     if(a<b){
    194         r=a;
    195         return m=0;
    196     }
    197     while(R>L){
    198         //L.print();
    199         //R.print();
    200         t=(R+L);
    201         devide_2(t);
    202         if(t==L){
    203             m=t*b;
    204             r=a-m;return L;
    205         }
    206         m=t*b;
    207         //m.print();
    208         if(m==a){
    209             r=0;return t;
    210         }else if(m<a){
    211             L=t;
    212         }else{
    213             R=t;
    214         }
    215     }
    216     m=t*b;
    217     r=a-m;
    218     return L;
    219 }
    220 inline BigInt operator%(BigInt &a,BigInt &b){
    221     BigInt r;
    222     devide(a,b,r);
    223     return r;
    224 }
    225 inline bool Is_even(const BigInt &a){
    226     if(a.s[0]%2==0)return true;
    227     return false;
    228 }
    229 //计算n的k次方模p
    230 BigInt qpow(BigInt &n,int k){
    231     BigInt ans,y;
    232     int _n=k;
    233     //n.print();k.print();p.print();
    234     ans=1;y=n;
    235     while(_n>=1){
    236         if(_n%2){
    237             ans=(ans*y);
    238             //ans.print();
    239             ans=ans;
    240             //ans.print();
    241         }
    242         y=y*y;
    243         //y.print();
    244         y=y;
    245         //y.print();
    246         //cout<<"---------
    ";
    247         _n/=2;
    248        // _n.print();
    249     }
    250     return ans;
    251 }
    252 BigInt p;
    253 int n,len;
    254 void solve(){
    255     BigInt l,r,m;
    256     l=pow(10.0,(double)len/n-1);
    257     r=pow(10.0,(double)len/n+1);
    258     if(qpow(r,n)==p){
    259         r.print();return;
    260     }
    261     while(1){
    262         m=l+r;
    263         devide_2(m);
    264         if(m==l){
    265             l.print();
    266             return;
    267         }
    268         //m.print();
    269         BigInt q=qpow(m,n);
    270         if(q==p){
    271             m.print();return;
    272         }
    273         if(q<p){
    274             l=m;continue;
    275         }else{
    276             r=m;continue;
    277         }
    278     }
    279 }
    280 int main(){
    281     //freopen("e:\in.txt","r",stdin);
    282     string s;
    283     while(cin>>n){
    284         cin>>s;
    285         len=s.length();
    286         p=s;
    287         solve();
    288     }
    289 
    290 }
    View Code

    E - Y2K Accounting Bug

     只有12个月,总的情况数是2^12次方,枚举一下.需要一些优化,比如目前可行解最多k个月盈利,那么小于等于k个月盈利的就可以不用算了.还可以通过s/d,比如s/d<=1/5,1/4<=s/d<=1/2都是不可能盈利的情况.

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 int m[12],s,d;
     6 int surplus(){
     7     int ans=0;
     8     int surmonth=0;
     9     for(int i=0;i<(1<<12);i++){
    10         int count1=0;
    11         for(int j=0;j<12;j++){
    12             count1+=(i&(1<<j))?1:0;
    13         }
    14         if(count1<=surmonth||count1*s-(12-count1)*d<=0)continue;
    15         for(int j=0;j<12;j++)
    16             m[j]=i&(1<<j);
    17             bool ok=true;
    18         for(int j=0;j<8;j++){
    19             int sur=0;
    20             for(int k=0;k<5;k++){
    21                 if(m[j+k])sur+=s;
    22                 else sur-=d;
    23             }
    24             if(sur>=0){
    25                 ok=false;break;
    26             }
    27         }
    28         if(ok){
    29             surmonth=count1;
    30             int sur=count1*s-(12-count1)*d;
    31             if(sur>ans)ans=sur;
    32         }
    33     }
    34     return ans;
    35 }
    36 int main(){
    37 //    freopen("e:\in.txt","r",stdin);
    38 //    freopen("e:\out.txt","w",stdout);
    39     while(cin>>s>>d){
    40 //        if((d<2*s&&3*s<2*d)||(d>4*s&&5*s>d))cout<<surplus()<<endl;
    41 //        else cout<<"Deficit"<<endl;
    42         int ans=surplus();
    43         if(ans)cout<<ans<<endl;
    44         else cout<<"Deficit"<<endl;
    45     }
    46     return 0;
    47 }
    View Code

    F - Ubiquitous Religions

    就是求连通块的个数,并查集求一下就行了..

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 const int maxn=50005;
     6 int n,m,p[maxn];
     7 int Find(int x){return p[x]==x?x:p[x]=Find(p[x]);}
     8 int main(){
     9     int kase=0;
    10     while(scanf("%d%d",&n,&m)!=EOF&&n){
    11         int count=n;
    12         for(int i=0;i<n;i++)p[i]=i;
    13         int x,y;
    14         for(int i=0;i<m;i++){
    15             scanf("%d%d",&x,&y);
    16             x--;y--;
    17             if(Find(x)==Find(y))continue;
    18             p[Find(x)]=y;
    19             count--;
    20         }
    21         printf("Case %d: %d
    ",++kase,count);
    22     }
    23     return 0;
    24 }
    View Code

    G - Tiling

     显然可以得到递推式f(n)=f(n-1)+2*f(n-2),n只有250,也不需要矩阵了,高精度打个表...

     1 import java.io.*;
     2 import java.util.*;
     3 import java.math.BigInteger;
     4 public class Main {
     5     public static void main(String arg[]){
     6         BigInteger a=BigInteger.valueOf(1),b=BigInteger.valueOf(1);
     7         String s[]=new String[260];
     8         s[0]="1";s[1]="1";
     9         for(int i=2;i<=255;){
    10             a=b.add(a).add(a);
    11             s[i++]=a.toString();
    12             b=a.add(b).add(b);
    13             s[i++]=b.toString();
    14         }
    15         Scanner sc=new Scanner(System.in);
    16         int n;
    17         while(sc.hasNext()){
    18             n=sc.nextInt();
    19             System.out.println(s[n]);
    20         }
    21     }
    22 
    23 }
    View Code

    H - Tautology

     只有5个小写字母,总的情况就是2^5,枚举一下是不是每次结果都为真就行了.式子的处理类似计算器,递归一下.

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 bool b[5]={0,0,0,0,0};
     5 int f(char c){
     6     switch(c){
     7     case 'p':return 0;
     8     case 'q':return 1;
     9     case 'r':return 2;
    10     case 's':return 3;
    11     case 't':return 4;
    12     }
    13 }
    14 bool Is_up(char c){
    15     if(c>='A'&&c<='Z')return true;
    16     return false;
    17 }
    18 bool Is_true(char *s){
    19     int count=0;
    20     int n=strlen(s);
    21     if(s[0]=='N')return !Is_true(s+1);
    22     if(!Is_up(s[0]))return b[f(s[0])];
    23     int mid=1;
    24     if(!Is_up(s[1]))mid=1;
    25     else{
    26         mid=1;
    27         for(int i=1;i<n;i++,mid++){
    28             if(s[i]=='N')count+=0;
    29             else if(Is_up(s[i]))count+=1;
    30             else count-=1;
    31             if(count==-1)break;
    32         }
    33     }
    34     switch(s[0]){
    35         case 'K':return Is_true(s+1)&&Is_true(s+mid+1);
    36         case 'A':return Is_true(s+1)||Is_true(s+mid+1);
    37         case 'C':return !(Is_true(s+1)==1&&Is_true(s+mid+1)==0);
    38         case 'E':return Is_true(s+1)==Is_true(s+mid+1);
    39     }
    40 }
    41 bool Is_tau(char *s){
    42     for(int i=0;i<32;i++){
    43         for(int j=0;j<5;j++)
    44             b[j]=i&(1<<j);
    45         if(!Is_true(s))return false;
    46     }
    47     return true;
    48 }
    49 int main(){
    50     char s[105];
    51     while(cin>>s&&s[0]!='0'){
    52         if(Is_tau(s))cout<<"tautology
    ";
    53         else cout<<"not
    ";
    54     }
    55     return 0;
    56 }
    View Code

    I - Parencodings

     P先转S,S再转W

     1 #include<iostream>
     2 using namespace std;
     3 const int maxn=25;
     4 int s[2*maxn],p[maxn],w[maxn],n;
     5 void p_s(){
     6     for(int i=n-1;i>0;i--)
     7         p[i]-=p[i-1];
     8     int j=0;
     9     for(int i=0;i<n;i++){
    10         while(p[i]){
    11             s[j++]=1;
    12             p[i]--;
    13         }
    14         s[j++]=0;
    15     }
    16 }
    17 void s_w(){
    18     int r[maxn],_n=0;
    19     for(int i=0;i<2*n;i++){
    20         if(s[i]==0)r[_n++]=i;
    21     }
    22     for(int i=n-1;i>=0;i--){
    23         int j=r[i]-1,count=1;
    24         while(j>=0&&count){
    25             if(s[j])count--;
    26             else count++;
    27             j--;
    28         }
    29         w[i]=(r[i]-j)/2;
    30     }
    31 }
    32 int main(){
    33     int t;
    34     cin>>t;
    35     while(t--){
    36         cin>>n;
    37         for(int i=0;i<n;i++)
    38             cin>>p[i];
    39         p_s();
    40         s_w();
    41         for(int i=0;i<n;i++){
    42             if(i)cout<<' ';
    43             cout<<w[i];
    44         }
    45         cout<<endl;
    46     }
    47     return 0;
    48 }
    View Code

    后面四道就是纯水模拟了...

    J - Crashing Robots

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 using namespace std;
      5 const int maxn=105;
      6 int house[maxn][maxn],A,B;
      7 char d[4]={'N','E','S','W'};
      8 char ok[100]="OK";
      9 char ans[100];
     10 int f(char c){
     11     switch(c){
     12         case 'N':return 0;
     13         case 'E':return 1;
     14         case 'S':return 2;
     15         case 'W':return 3;
     16     }
     17 }
     18 //-1ÊÇǽ
     19 int Is_crash(int x,int y){
     20     if(x==0||x==A+1||y==0||y==B+1)return -1;
     21     return house[x][y];
     22 }
     23 class robot{
     24     int x,y,order;
     25     char dir;
     26 public:
     27     void input(int t){
     28         cin>>x>>y>>dir;
     29         order=t;
     30         house[x][y]=t;
     31     }
     32     void move(char c,int time){
     33         if(strcmp(ans,"OK"))return;
     34         switch(c){
     35             case 'L':dir=d[(f(dir)-time%4+4)%4];break;
     36             case 'R':dir=d[(f(dir)+time)%4];break;
     37             default:switch(dir){
     38             case 'N':
     39                 for(int i=0;i<time;i++){
     40                     y++;
     41                     int k=Is_crash(x,y);
     42                     if(k==-1){sprintf(ans,"Robot %d crashes into the wall",order);break;}
     43                     else if(k){sprintf(ans,"Robot %d crashes into robot %d",order,k);break;}
     44                     house[x][y-1]=0;
     45                     house[x][y]=order;
     46                 }
     47                 break;
     48             case 'S':
     49                 for(int i=0;i<time;i++){
     50                     y--;
     51                     int k=Is_crash(x,y);
     52                     if(k==-1){sprintf(ans,"Robot %d crashes into the wall",order);break;}
     53                     else if(k){sprintf(ans,"Robot %d crashes into robot %d",order,k);break;}
     54                     house[x][y+1]=0;
     55                     house[x][y]=order;
     56                 }
     57                 break;
     58             case 'E':
     59                 for(int i=0;i<time;i++){
     60                     x++;
     61                     int k=Is_crash(x,y);
     62                     if(k==-1){sprintf(ans,"Robot %d crashes into the wall",order);break;}
     63                     else if(k){sprintf(ans,"Robot %d crashes into robot %d",order,k);break;}
     64                     house[x-1][y]=0;
     65                     house[x][y]=order;
     66                 }
     67                 break;
     68             case 'W':
     69                 for(int i=0;i<time;i++){
     70                     x--;
     71                     int k=Is_crash(x,y);
     72                     if(k==-1){sprintf(ans,"Robot %d crashes into the wall",order);break;}
     73                     else if(k){sprintf(ans,"Robot %d crashes into robot %d",order,k);break;}
     74                     house[x+1][y]=0;
     75                     house[x][y]=order;
     76                 }
     77                 break;
     78             }
     79         }
     80     }
     81 }rb[maxn];
     82 int main(){
     83     //freopen("e:\in.txt","r",stdin);
     84     //freopen("e:\out.txt","w",stdout);
     85     int t,n,m,num,time;
     86     char mo;
     87     cin>>t;
     88     while(t--){
     89         strcpy(ans,ok);
     90         memset(house,0,sizeof(house));
     91         cin>>A>>B>>n>>m;
     92         for(int i=1;i<=n;i++){
     93             rb[i].input(i);
     94         }
     95         for(int i=0;i<m;i++){
     96             cin>>num>>mo>>time;
     97             rb[num].move(mo,time);
     98         }
     99         cout<<ans<<endl;
    100     }
    101     return 0;
    102 }
    View Code

    K - Robot Motion

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 using namespace std;
     5 const int maxn=15;
     6 char grid[maxn][maxn];
     7 int step[maxn][maxn],x,y,total,n,m,enter;
     8 void solve(){
     9     memset(step,-1,sizeof(step));
    10     total=0;x=1;y=enter;
    11     step[x][y]=0;
    12     while(1){
    13         switch(grid[x][y]){
    14             case 'N':x--;break;
    15             case 'S':x++;break;
    16             case 'W':y--;break;
    17             case 'E':y++;break;
    18         }
    19         total++;
    20         if(x==0||x==n+1||y==0||y==m+1){
    21             cout<<total<<" step(s) to exit"<<endl;return;
    22         }
    23         if(step[x][y]!=-1){
    24             cout<<step[x][y]<<" step(s) before a loop of "<<total-step[x][y]<<" step(s)"<<endl;return;
    25         }
    26         step[x][y]=total;
    27     }
    28 }
    29 int main(){
    30     while(cin>>n>>m>>enter&&n){
    31         for(int i=1;i<=n;i++){
    32             for(int j=1;j<=m;j++)
    33                 cin>>grid[i][j];
    34         }
    35         solve();
    36     }
    37     return 0;
    38 }
    View Code

    L - Emag eht htiw Em Pleh

     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 using namespace std;
     5 char ans[20][35];
     6 bool Is_up(char c){
     7     if(c>='A'&&c<='Z')return true;
     8     return false;
     9 }
    10 int main(){
    11     for(int i=0;i<=16;i+=2){
    12         strcpy(ans[i],"+---+---+---+---+---+---+---+---+");
    13     }
    14     for(int i=3;i<=15;i+=4){
    15         strcpy(ans[i],"|:::|...|:::|...|:::|...|:::|...|");
    16     }
    17     for(int i=1;i<=15;i+=4){
    18         strcpy(ans[i],"|...|:::|...|:::|...|:::|...|:::|");
    19     }
    20     string s;
    21     cin>>s;
    22     cin>>s;
    23     for(int i=0;i<s.size();){
    24         if(Is_up(s[i])){
    25             ans[17-2*(s[i+2]-'0')][4*(s[i+1]-'a')+2]=s[i];
    26             i+=4;
    27         }else{
    28             ans[17-2*(s[i+1]-'0')][4*(s[i]-'a')+2]='P';
    29             i+=3;
    30         }
    31     }
    32     cin>>s;
    33     cin>>s;
    34     for(int i=0;i<s.size();){
    35         if(Is_up(s[i])){
    36             ans[17-2*(s[i+2]-'0')][4*(s[i+1]-'a')+2]=s[i]-'A'+'a';
    37             i+=4;
    38         }else{
    39             ans[17-2*(s[i+1]-'0')][4*(s[i]-'a')+2]='p';
    40             i+=3;
    41         }
    42     }
    43     for(int i=0;i<=16;i++){
    44         cout<<ans[i]<<endl;
    45     }
    46     return 0;
    47 }
    View Code

    M - Help Me with the Game

     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<vector>
     5 #include<algorithm>
     6 using namespace std;
     7 vector<int> w[26];
     8 vector<int> b[26];
     9 char order[]="KQRBN";
    10 bool Cmpw(int i,int j){
    11     if(i%16==j%16)return i/16<j/16;
    12     return i%16<j%16;
    13 }
    14 bool Cmpb(int i,int j){
    15     if(i%16==j%16)return i/16<j/16;
    16     return i%16>j%16;
    17 }
    18 bool Is_up(char c){
    19     if(c>='A'&&c<='Z')return true;
    20     return false;
    21 }
    22 bool Is_low(char c){
    23     if(c>='a'&&c<='z')return true;
    24     return false;
    25 }
    26 int main(){
    27     string s;
    28     for(int i=0;i<26;i++){
    29         w[i].clear();
    30         b[i].clear();
    31     }
    32     for(int i=8;i>=1;i--){
    33         getline(cin,s);
    34         getline(cin,s);
    35         for(int j=2;j<=30;j+=4){
    36             if(Is_up(s[j])){
    37                 w[s[j]-'A'].push_back((j-2)*4+i);
    38             }else if(Is_low(s[j])){
    39                 b[s[j]-'a'].push_back((j-2)*4+i);
    40             }
    41         }
    42     }
    43     getline(cin,s);
    44     cout<<"White: ";
    45     for(int i=0;i<5;i++){
    46         char c=order[i];
    47 
    48         for(int j=0;j<w[c-'A'].size();j++){
    49             if(i||j)cout<<',';
    50             cout<<c<<char(w[c-'A'][j]/16+'a')<<w[c-'A'][j]%16;
    51         }
    52     }
    53     sort(w['P'-'A'].begin(),w['P'-'A'].end(),Cmpw);
    54     for(int i=0;i<w['P'-'A'].size();i++){
    55         cout<<','<<char(w['P'-'A'][i]/16+'a')<<w['P'-'A'][i]%16;
    56     }
    57     cout<<endl;
    58     cout<<"Black: ";
    59     for(int i=0;i<5;i++){
    60         char c=order[i];
    61         for(int j=0;j<b[c-'A'].size();j++){
    62             if(i||j)cout<<',';
    63             cout<<c<<char(b[c-'A'][j]/16+'a')<<b[c-'A'][j]%16;
    64         }
    65     }
    66     sort(b['P'-'A'].begin(),b['P'-'A'].end(),Cmpb);
    67     for(int i=0;i<b['P'-'A'].size();i++){
    68         cout<<','<<char(b['P'-'A'][i]/16+'a')<<b['P'-'A'][i]%16;
    69     }
    70     cout<<endl;
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    HDU 3951 (博弈) Coin Game
    HDU 3863 (博弈) No Gambling
    HDU 3544 (不平等博弈) Alice's Game
    POJ 3225 (线段树 区间更新) Help with Intervals
    POJ 2528 (线段树 离散化) Mayor's posters
    POJ 3468 (线段树 区间增减) A Simple Problem with Integers
    HDU 1698 (线段树 区间更新) Just a Hook
    POJ (线段树) Who Gets the Most Candies?
    POJ 2828 (线段树 单点更新) Buy Tickets
    HDU 2795 (线段树 单点更新) Billboard
  • 原文地址:https://www.cnblogs.com/7391-KID/p/7056603.html
Copyright © 2020-2023  润新知