2017年3月18日 星期六

Y7M3 P2A 練習-5

100北二區資訊複賽

1.圈叉連線數問題(zj:a817)      ✔(上次已傳)

2.藏寶問題(zj:a824)                ✔(上次已傳)

3.小四數學(zj:a825)                ✔(上次已傳)

4.天氣預測問題(zj:a826)          ✔(上次已傳)

5.俄羅斯方塊問題(zj:a827)

101北二區資訊複賽

1.解碼問題(zj:a818)                  ✔(上次已傳)

2.小三資優數學(zj:a819)

3.小精靈吃數字(zj:a820)            ✔(上次已傳)

4.王者之路(zj:a821)                  ✔

5.滾球遊戲(zj:a822)                   ✔

101年4.王者之路(zj:a821)  ================ 
// a821. 4.王者之路 -- 101學年度桃竹苗區資訊學科能力競賽
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
const int MaxN = 20; //最多 20 個
map<string , int> mp;  //姓名,編號 :編號為 1 ~ n
int mpcnt;   // 已出現的隊伍數
bool los[MaxN+1]; 
string nam[MaxN+1];
int main(void)
{
int  n , r , i;
string a,b;
while( cin >> n >> r )
{
mpcnt = 0;   mp.clear();

for(i=1; i<=n; ++i) los[i] = 0;
for(i=0; i<r; ++i)
{
cin >> a >> b;
if( mp.find(a) == mp.end() ) mp[a] = ++mpcnt; //  mp.size(); //
int ai = mp[a]; nam[ai] = a;
if( mp.find(b) == mp.end() ) mp[b] = ++mpcnt; // mp.size(); //
int bi = mp[b]; nam[bi] = b;
los[bi] = 1;  //輸過一場就是 1
}
// 因為 王者 會贏其他所有的人,不會輸
for(i=1; i<=n; ++i) 
     if( los[i] == 0 ){   // 沒輸過的就是王者
           cout << nam[i] << endl;
            break;

     }
}
return 0;
}

/*  ----範例輸入
3 3
tpa azf
tpa abc
azf abc
5 11
tpa azf
azf abc
tpa abc
abc xyz
tpa abcde
xyz abc
azf abcde
azf xyz
abc azf
tpa xyz
azf abcde
5 12
mmm bbb
bbb ccc
mmm ccc
ddd mmm
mmm eee
ddd ccc
ddd bbb
bbb mmm
ddd eee
bbb eee
mmm bbb
bbb ccc
-----------範例輸出
tpa
tpa
ddd
*/


101年5.滾球遊戲(zj:a822) 解1 ================ 
// zj: a822 滾球遊戲  -- 101學年度桃竹苗區資訊學科能力競賽
#include <iostream>
#include<cstring>
using namespace std;
int gb[500][500]; // 遊戲盤 game board 
int bn[500][500]; // 各格球數 ball numbers 
int n , n2;       // n2 = n*n;
void game(int r, int c)
{
   memset (gb,0,sizeof(gb));
   int i,j, t=0;
   while( t<n2 )
   {
      gb[r][c]= ++t;
      if(t==n2) break;
      i=(r*n+c+t)%n2;  //下一格的序號,所有格子換算成 0~n^2-1
      r=i/n  ; c=i%n;  // 序號轉回 列、行 (r,c)
      while(gb[r][c])  // 若該格已填入過, 往後找到可用的空格
      {
         c++;         // 往右找,一列找完往下一列
         if(c>=n)
         {
            ++r; c=0;
            if(r>=n) r=0;   // 最後一列回到第1列
         }
      }    
   }
}
int mr[]={-1,1,0,0};
int mc[]={0,0,-1,1};
void putball(int r, int c)
{
   int m=gb[r][c];
   int nr,nc, nd;
   while( 1 ) // 滾至最低格才停 
   {
      nd = -1;  // 是否為最小(最低格)
      for(int d=0; d<4; ++d) // 上下左右
      {
         nr = r+mr[d];  nc = c+mc[d];
         if(nr<0 || nr>=n || nc<0 || nc>=n) continue;
         if( gb[nr][nc] < m ) 
         {
            m=gb[nr][nc];  nd=d;  // m 四周 5格中 最小數字
         }     
      }
      if( nd == -1 )  break; //已是最低格了
      r = r+mr[nd];  c= c+mc[nd];  //移至四周較小的 格子
   }
   ++ bn[r][c];  //球停在此格
}

int main( )
{

   int  r,c;   // n , n2 公用變數 
   cin >> n >> r >> c;
   n2 = n*n;
   game(r,c);  // 產生遊戲盤
// 每1格放1球,滾至某格,將 bn[nr][nc] + 1;
   memset ( bn,0,sizeof(bn) ); 
   for(r=0;r<n;++r)
      for(c=0;c<n;++c)
         putball(r,c);
// 計算有哪幾格有球,最多球的球數?
   int gc=0 , bc=0;
   for(r=0;r<n;++r)
      for(c=0;c<n;++c)
      {
         if(bn[r][c])
         {
            ++gc;   // 格子數+1
            if(bn[r][c] > bc) bc=bn[r][c];
         }
      } 
   cout << gc <<" " << bc << endl;
   return 0 ;
}
101年5.滾球遊戲(zj:a822) 解2 ================ 
// zj: a822 滾球遊戲  -- 101學年度桃竹苗區資訊學科能力競賽
#include <iostream>
#include <cstring>
using namespace std;
const int MaxN = 500;
int gb[MaxN][MaxN];  // 遊戲盤數值
int bc[MaxN][MaxN];  // 各格球數
int n , n2 ;
void setgb(int r, int c)
{
int t=1 , p;
while( t< n2 )
{
gb[r][c] = t;
p = ( r*n + c + t++ ) % n2;
r = p/n;  c=p%n;
while( gb[r][c] !=0 )
{
p = (p+1)%n2; //找 下一 空格; 
r = p/n;  c=p%n;
}
}
gb[r][c] = t;
}
int dr[] = {-1, 1, 0, 0};
int dc[] = { 0, 0,-1, 1};
void putball(int r, int c)
{
int m=gb[r][c];
int ur =r , uc=c;
for(int d=0; d<4; ++d)
{
int nr=r+dr[d] , nc=c+dc[d];
if( nr<0 || nr>=n || nc<0 || nc>=n ) continue;
if( gb[nr][nc] < m ) 
{
ur=nr;  uc=nc;  m=gb[nr][nc];
}
}
if( ur==r && uc==c )  bc[r][c]++; 
else putball(ur,uc);  // 四周有更低格
}
void print(int x[][MaxN] )
{
  int i,j;
for(i=0; i<n; ++i)
{ 
  for(j=0; j<n; ++j)
    cout << x[i][j] <<' ';
  cout << endl;
}

}
int main(void)
{
int r,c,p;

while( cin >> n >> r >> c ) // 讀至檔尾
{
memset(gb,0,sizeof(gb));
memset(bc,0,sizeof(bc));
n2 = n*n;
setgb(r,c);  // 產生遊戲盤
// print(gb);   // 測試列印用 //
for(r=0; r<n; ++r)
 for(c=0; c<n; ++c)
   putball(r,c) ;  // 在(r,c)放1球
// print(bc);   // 測試列印用 //
// 找出共幾格有球ans1,球最多的格子有幾球ans2
  int ans1=0 , ans2=0;
for(r=0; r<n; ++r)
 for(c=0; c<n; ++c)
   if(bc[r][c])
   {
     ans1++;  // 多一格有球
     if(bc[r][c]>ans2) ans2=bc[r][c];
   }
   cout << ans1 << ' ' << ans2 << endl;
}
return 0;
}

/* ----範例輸入
3 1 1
4 0 0
-------範例輸出
1 9
5 5
*/

    2017年3月13日 星期一

    Y7M3 P2A練習4

    100北二區資訊複賽

    1.圈叉連線數問題(zj:a817)      ✔(上次已傳)

    2.藏寶問題(zj:a824)                ✔(上次已傳)

    3.小四數學(zj:a825)                ✔(上次已傳)

    4.天氣預測問題(zj:a826)         ✔

    5.俄羅斯方塊問題(zj:a827)

    101北二區資訊複賽

    1.解碼問題(zj:a818)                  ✔(上次已傳)

    2.小三資優數學(zj:a819)

    3.小精靈吃數字(zj:a820)          ✔

    4.王者之路(zj:a821)

    5.滾球遊戲(zj:a822)

    100年4.天氣預測問題(zj:a826)  ================ 
    // a826. 4.天氣預測問題 -- 100學年度桃竹苗區資訊學科能力競賽
    #include <iostream>
    #include <iomanip>
    using namespace std;
    string stbl[]={"cool" , "mild", "hot", "dry" , "normal" , "high" };
    struct rst {
    int m;
    int v;
    int y;
    }  rule[5];
    int stov(int m, string s)
    {
    for(int i=0;i<3;++i)
    {
    if( s==stbl[m*3+i] ) return i;
       }
    }
    int g[2] ;
    int chk(int n)
    {
       for(int i=0; i<n; ++i)
       {
          if( g[ rule[i].m ] == rule[i].v ) return (i+1);
       }
       return 0;   
    }
    int main( )
    {

       int ges[10][3] = { {1,0,1} ,  {1,1,1} , {1,0,1} , {2,2,0} , {2,2,0} ,
        {2,2,0} , {2,2,0} , {2,0,0} , {2,1,0} , {0,0,1} };
    int t ,h, n  ,m ,i , j , k;
    char c , y;
    string s;
    cin >> n;
    for(i=0; i<n; ++i)
    {  
    cin >> c >> s >>y;
    rule[i].m = (c=='H');
    rule[i].v = stov(rule[i].m , s );
    rule[i].y = (y=='Y');
    }
       int  cnt=0;
    for(j=0; j<10; ++j)
    {  
          g[0] = ges[j][0];
          g[1] = ges[j][1];
          i = chk(n);
     //     cout << g[0] <<" " << g[1]  <<" "<< i  <<":" << rule[i-1].y <<" " <<ges[j][2] <<endl;
          if( i )  cnt += ( rule[i-1].y == ges[j][2] );
          else cnt += ( !ges[j][2] );
    }
       cin >> t >> h;
       g[0] = (t>=20) + (t>=28);
       g[1] = (h>=30) + (h>=60);
       i = chk(n);
       cout << cnt << endl;
       cout << i << endl;
       if(i) cout << ( rule[i-1].y ? 'Y' : 'N' ) << endl;
       else cout << "N" << endl;
       
       return 0;
    }

    /*  ----範例輸入
    (之一)
    2
    H dry Y
    T hot N
    30 50
    (之二)
    2
    T mild Y
    T hot N
    25 30
    -----------範例輸出
    (之一)
    8
    2
    N
    (之二)

    9
    1
    Y

    */


    101年3.小精靈吃數字(zj:a820) 解1 (x,y)在線上================ 
    // zj: a820 小精靈吃數字  -- 101學年度桃竹苗區資訊學科能力競賽
    #include <iostream>
    using namespace std;
    const int maxn=10;
    int a[maxn][maxn];
    int dx[4]={-1, 1, 1,-1};
    int dy[4]={ 1, 1,-1,-1};
    int nd[4][2][2] = { {0,3,1,2}, {1,2,0,3}, {2,1,3,0}, {3,0,2,1} };  //新轉向表
    int main( )
    {
       int w,h,i,j,k ;
      int x,y,d,d2 ,nx,ny;
      while( cin >> w >> h )
      {
     for(i=0; i<h; ++i)
      for(j=0; j<w; ++j)
         cin >> a[j][i];
     cin >> x >> y >> d;
     --d;  // 改為 0~3,並x,y改為0,0為左上
     if(dx[d]>0) --x;   if(dy[d]>0) --y;
     int ans=0 ;
     for(int cnt=0; cnt<=3; ++cnt)
     {
        while( 1 )
        {
           nx=x+dx[d];  ny=y+dy[d];
           d2 = nd[d][ d%3?nx>w:nx<0 ] [ d>1?ny<0:ny>h ];
    /*
    switch(d)  // 新的方向
    {
    case 0: d2=nd[d][nx<0][ny>h];
    break;
    case 1: d2=nd[d][nx>w][ny>h];
    break;
    case 2: d2=nd[d][nx>w][ny<0];
    break;
    case 3: d2=nd[d][nx<0][ny<0];
    break;
    }
    */
           if( d==d2) { // 方向不變
              ans += a[(x+nx)/2][(y+ny)/2];
              x=nx; y=ny;
           } else {
           d=d2;  break;  // 出界
           }
        }
     }
     cout << ans << endl;  
      }
      return 0;
    }

    101年3.小精靈吃數字(zj:a820) 解2 (x,y)在格子內================ 
    // zj: a820 小精靈吃數字  -- 101學年度桃竹苗區資訊學科能力競賽

    /*
    第一列 X格,Y列(沒明確說明,假設10x10),第二列至第(Y+1)列皆有(X)個數{且-9<=*<=9},第(Y+2)列為起點(SX,SY)及方向1右上2左上3左下4右下
    反彈三次後,即第4次不反彈離開,問經過的數值和
    */
    #include <iostream>
    using namespace std;
    const int MaxN = 10;
    int d, r;  //d行進方向, r反彈類型
    int dx[]={-1, 1, 1,-1};  //0從右上往左下、1左上、2左下、3右下
    int dy[]={1 , 1,-1,-1};  // (dx,dy) 行進的位移
    int rx[]={ 0,0,-1,1  ,0};  //0上、1下、2左、3右    、4四角
    int ry[]={-1,1, 0,0  ,0};  //四邊反彈後(x,y)+(rx,ry)的位移{非四角}
    int a[MaxN+1][MaxN+1];
    int main(void)
    {
    int i,j, x,y,nx,ny;
       int EX,EY;
    while( cin >> EX >> EY )
    {
    for(i=1; i<=EY; ++i)
     for(j=1; j<=EX; ++j)
       cin >> a[i][j];
    // 接著讀進入點及方向
    cin >> x >> y >> d;
    // cout << "由"<<x<<','<<y<<':'<<d<<"進入\n";
    d-- ; //改為 0~3  
    nx = x; ny=y;
    int t=0; //反彈次數
    int sum=0; // 數字和
    while( t<4 )
    {
    x=nx;  y=ny; // x,y原座標,
    sum += a[y][x];
    // cout <<'('<<x<<','<<y<<'='<<a[y][x]<<endl;
    nx = x+dx[d];  ny=y+dy[d];
    bool xx=(nx<1 || nx>EX) ; //左右 x座標出界
    bool yy=(ny<1 || ny>EY) ; //上下 y座標出界
    if( xx || yy ) // 有出界,會反彈
    {
    if ( xx && yy ) { r=4;  d = (d+2)%4; } //四角的反彈
    else  // 四邊反彈
    {
    if(xx && (d==2 || d==3) )   {r=0; d=5-d; }  // 2,3互換
    else if(xx && (d==0 || d==1) ) {r=1; d=1-d; }  // 0,1互換
    else if(yy && (d==0 || d==3) ) {r=2; d=3-d; }  // 0,3互換
    else if(yy && (d==1 || d==2) ) {r=3; d=3-d; }  // 1,2互換
    else ; //???
    }
    // 算出 邊反彈後的新座標
    nx = x+rx[r] ;  ny = y+ry[r];
    t++;  //反彈加1
    }
    //else  nx,ny 在界內繼續走
    }
    cout << sum << endl;

    }

    return 0;
    }

    /* ----範例輸入
    3 3
    1 3 9
    -4 -6 5
    8 2 -7
    3 1 1
    3 4
    2 5 8
    3 -1 -6
    7 8 -9
    2 0 3
    1 1 2
    -------範例輸出
    44
    10
    */