19. 大吉大利,晚上吃雞
阿新 • • 發佈:2018-12-26
Description
眾所周知,現在吃雞遊戲非常流行,龍神也想在這個大流中撈一手,於是他也發明了一種吃雞遊戲。但是他非常討厭伏地魔,想要有著完全制約伏地魔的手段,於是他決定給遊戲裡空投按鈕,只要按下這個按鈕,就可以直接吃雞。但這樣遊戲不夠混亂沒有觀賞性。所以龍神決定讓所有玩家都不可以攻擊,只能通過按下按鈕來取勝,並且他決定空投許多個這樣的按鈕,只要按下其中之一就可以吃雞。 地圖是一個矩陣,玩家每秒只能移動到相鄰的格子中去。每個玩家都知道離自己最近的按鈕在哪,並且他們都很想贏。 龍神現在想知道每一局都在第幾秒結束。
本來題目是這樣的,可是龍神說這個題目太簡單了,所以他還想知道遊戲勝利的那個人走的路徑。如果有多種路徑輸入其中任意一條就可以了。
Input
#include <queue> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn = 1005; const int dx[] = {1, -1, 0, 0};//右左下上 const int dy[] = {0, 0, 1, -1}; struct node { int x, y; int step; // 初始化列表 node(int _x, int _y, int _step) : x(_x), y(_y), step(_step) {} }; queue<node> que; int vis[maxn][maxn]; char mp[maxn][maxn]; int pre[10000000]; int main() { int n, m, a, b; int p_x, p_y; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%s", &mp[i]); } // 又是一套初始化 memset(vis, 0, sizeof(vis)); int ans, count = 0, an_x, an_y; scanf("%d %d", &a, &b); for (int i = 0; i < a;i++) { scanf("%d %d", &p_x, &p_y); if(mp[p_x-1][p_y-1] == '#') count++; else mp[p_x - 1][p_y - 1] = 'P'; } if(count == a){ printf("Cruel game\n"); return 0; } while(b--) { scanf("%d %d", &an_x, &an_y); if(mp[an_x - 1][an_y - 1] == '#') ; else if(mp[an_x - 1][an_y - 1] == 'P'){ printf("0\n%d %d\n", an_x, an_y); return 0; } else{ que.push(node(an_x - 1, an_y - 1, 0)); vis[an_x-1][an_y-1] = 1; } } int end_x, end_y, flag = 0,t; while (!que.empty()) { node tmp = que.front();//tmp為取出來的待處理的隊首struct node型別的元素 que.pop(); if (mp[tmp.x][tmp.y] == 'P') { ans = tmp.step; end_x = tmp.x; end_y = tmp.y; flag = 1; break; } for (int i = 0; i < 4; ++i) { int x = tmp.x + dx[i], y = tmp.y + dy[i]; if (x < 0 || x >= n || y < 0 || y >= m || mp[x][y] == '#' || vis[x][y] == 1) { continue; } vis[x][y] = 1; pre[x * 10000 + y] = tmp.x * 10000 + tmp.y; que.push(node(x, y, tmp.step + 1)); } } if(flag){ printf("%d\n", ans); while(ans-->=0){ printf("%d %d\n", end_x+1, end_y+1); t = pre[end_x * 10000 + end_y] / 10000; end_y = pre[end_x * 10000 + end_y] % 10000; end_x = t; } } else{ printf("Peaceful world\n"); return 0; } return 0; }