【 OJ 】 HDOJ1057 18年12月22日15:37 [ 49 ]
阿新 • • 發佈:2018-12-23
Limit Time
題意:有一個20X20的網格,每個網格里有個數字(0~3),每天網格里數字的變化是先將網格里本身的數字與其上下左右網格里數字依次相加,得到一個0~15的值k,這個值確定了題目裡陣列D[k],接下來我們只需要把網格里的值與D[k]相加,如果相加後的值大於3,變化後的值就為3,小於0則就為0。最後再根據題目將數字變成對應的字元。
# include<iostream> # include<algorithm> # include<queue> #pragma warning (disable:4996) # define BAN 20 using namespace std; int D[16]; int map[BAN][BAN];//地圖 int map_t[BAN][BAN]; char L[4] = { '.','!','X','#' };//格式參照 int dri[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };//東南西北 左下右上 struct node { int x, y; node(int x_, int y_) { x = x_; y = y_; } node() {} }NeedRevise[BAN*BAN]; int NRIndex; queue<node>q; void count(const node&t) { int x, y; x = t.x; y = t.y; int sum = 0; q.push(t); for (int i = 0; i < 4; i++) { x += dri[i][0]; y += dri[i][1]; if ((x >= 0 && x < BAN) && (y >= 0 && y < BAN)) { sum += map[x][y]; } x = t.x; y = t.y; }//四向搜尋 sum += map[t.x][t.y];//加上自身 //((D[sum] + map[t.x][t.y] >= 3 )? (map[t.x][t.y] = 3) : (D[sum] + map[t.x][t.y] < 0 ? map[t.x][t.y] = 0 : map[t.x][t.y] = (D[sum] + map[t.x][t.y]) ));//計算出當前位置培養皿的值 if (D[sum] + map[t.x][t.y] >= 3)map_t[t.x][t.y] = 3; else { if (D[sum] + map[t.x][t.y] < 0)map_t[t.x][t.y] = 0; else map_t[t.x][t.y] = D[sum] + map[t.x][t.y]; } } void Fcheck(node&t) {//四向模擬 count(t);//先計算自己 NeedRevise[NRIndex++] = t; int x, y; x = t.x; y = t.y;//xy復位 for (int i = 0; i < 4; i++) { x += dri[i][0]; y += dri[i][1]; if ((x >= 0 && x < BAN) && (y >= 0 && y < BAN)) { node t_n; t_n.x = x; t_n.y = y; count(t_n); //count(node(x,y)); NeedRevise[NRIndex++] = t_n;//當前結點資訊需要更新 } x = t.x; y = t.y; }//四向搜尋 } int main(void) { //long start_time = GetTickCount(); //獲取此程式段開始執行時間 int N;cin >> N; int day,i,j; while (N--) { scanf("%d", &day); for (i = 0; i < 16; i++) cin >> D[i];//錄入D[k]資訊 for (i = 0; i < BAN; i++) for (j = 0; j < BAN; j++) { scanf( "%d",&map[i][j]); if (map[i][j]) { node tn; tn.x = i; tn.y = j; q.push(tn); }//將有細菌的地方入隊 }//錄入培養皿資訊 while (day--) { int qsize = q.size(); NRIndex = 0;//一天過後復原0 node t; while (qsize--) { t = q.front(); q.pop();//四向檢查入隊 Fcheck(t);//內部修改map值 }//模擬感染 for (i = 0; i < NRIndex; i++) { t = NeedRevise[i]; map[t.x][t.y] = map_t[t.x][t.y]; }//更新一天的資訊 }//模擬天數 for (i = 0; i < BAN; i++) { for (j = 0; j < BAN; j++) { printf("%c ", L[map[i][j]]); //cout << L[map[i][j]] << " "; }//模擬輸出培養皿的樣子 printf("\n"); } } //long end_time = GetTickCount(); //獲取此程式段開始執行時間 //cout << endl << "程式段執行時間:" << (end_time - start_time) << "ms!" << endl; //差值即執行時間 system("pause"); return 0; }