2016年8月6日 星期六

北二區101-3小精靈吃數字

/*
a820-y6m8 北二區101年第3題:小精靈吃數字
第一列 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};  // 某方向行進的位移
int rx[]={ 0,0,-1,1  ,0};  //0上、1下、2左、3右    、4四角
int ry[]={-1,1, 0,0  ,0};  //四邊反彈的位移{非四角}
int a[MaxN+1][MaxN+1];
int main(void)
{
int i,j, x,y,nx,ny;
   int EX,EY;
while( cin >> EX >> EY ) //EY列、EX格
{
for(i=1; i<=EY; ++i)
 for(j=1; j<=EX; ++j)
   cin >> a[i][j];  // [y][x]
// 接著讀進入點及方向
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 ) //反彈第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; }
else if(xx && (d==0 || d==1) ) {r=1; d=1-d; }
else if(yy && (d==0 || d==3) ) {r=2; d=3-d; }
else if(yy && (d==1 || d==2) ) {r=3; d=3-d; }
// else ; //??? , 應不會執行到
}
// 算出 邊反彈後的新座標
nx = x+rx[r] ;  ny = y+ry[r];
t++;  //反彈加1
}
//else  nx,ny 在界內繼續走
}
cout << sum << endl;

}

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

Related Posts:

  • 找零使用最少硬幣(CPP版參考解)輸入第一列 t 代表有 t 組資料,每組2列:第1列  n 種 ,  m元,第2列 n 種的面額範例輸入:35 501 3 5 7 135 501 5 6 7 158 1251 2 3 10 11 12 20 21範例輸… Read More
  • 北二區101-2小三數學(C++版) 參考程式碼: #include <iostream> using namespace std; typedef long long LL; const LL INF = 1e18; int n,m , cnt; string s; LL a,b,c; LL cal(LL x… Read More
  • 北二區101-4王者之路(C++版) 參考程式碼 : #include <iostream> #include <cstring> #include <map> using namespace std; const int MaxN = 20; //最多 20 個 map<strin… Read More
  • 萬球同心移位(C++參考)// 測試資料改為第1行為 t 組,0<t<9,接著每組 第1列n  m ,接著 m列 /*  有n個球編號0~n-1{ 3<n<5*10^5},先將n個球依順時鐘圍成一圈 m個指令F A B 或R A B {2<m<10^3} F A … Read More
  • 找零使用最少硬幣數 設有5種硬幣:1, 5, 6, 7, 15:找零50元使用最少數量的硬幣 先設定1~50中只需要1個及2個硬幣的數額;1個:1, 5, 6, 7, 15 2個:2{1+1},8{1+7},16{1+15},10{5+5},11{5+6}… Read More

0 意見:

張貼留言