AOJ-ICPC 2253
Brave Force Story | Aizu Online Judge
普段4方向のものが6方向になっただけの迷路問題。
ところどころ障がいがある中で、決められたターンの中でどこまで進めるかを求めます。
幅優先探索で解きました。
#include <cstdio> #include <iostream> #include <cmath> #include <ctype.h> #include <string> #include <sstream> #include <iostream> #include <algorithm> #include <cstdlib> #include <map> #include <queue> #include <utility> #include <vector> #include <set> using namespace std; int dx[6] = {-1, -1, 0, 1, 1, 0}; int dy[6] = {0, -1, -1, 0, 1, 1}; bool issearched[140][140]; int t, n; int islandmap[140][140]; int tesu[140][140]; int start_x, start_y; void search(int a, int b); int main() { while(1){ cin >> t >> n; if(t == 0 && n == 0) break; for(int i = 0; i < 140; i++){ for(int j = 0; j < 140; j++){ islandmap[i][j] = 0; issearched[i][j] = false; tesu[i][j] = 0; } } int obstacle_x, obstacle_y; for(int i = 0; i < n; i++){ cin >> obstacle_x >> obstacle_y; islandmap[obstacle_x + 70][obstacle_y + 70] = -1; } cin >> start_x >> start_y; issearched[start_x + 70][start_y + 70] = true; search(start_x + 70, start_y + 70); int ans = 1; for(int i = start_x + 70 - t; i < start_x + 71 + t; i++){ for(int j = start_y + 70 - t; j < start_y + 71 + t; j++){ if(tesu[i][j] < t + 1 && tesu[i][j] > 0) ans++; } } cout << ans << endl; } return 0; } void search(int a, int b) { int x, y, nx, ny; queue<pair < int, int> > qu; qu.push(make_pair<int, int>(a, b)); while(!qu.empty()){ x = (qu.front()).first; y = (qu.front()).second; for(int i = 0; i < 6; i++){ nx = x + dx[i]; ny = y + dy[i]; if(nx < start_x + 70 - t|| ny < start_y + 70 - t|| nx > start_x + 71 + t|| ny > start_y + 71 + t) continue; if(islandmap[nx][ny] != -1 && issearched[nx][ny] == false){ tesu[nx][ny] = tesu[x][y] + 1; qu.push(make_pair<int, int>(nx, ny)); issearched[nx][ny] = true; } } qu.pop(); } return; }
特に何も言うことはないかなあ。
islandmapをどういう風に取るか苦労した。とりあえず(0, 0)がisland[70][70]になるようにした。
最初は[35][35]とかにしててsegmentation faultでんんんんってなりました。