赞
踩
题目:
思路:
把草的初始位置加入队列,开始广搜, 把遇到的每一块坐标合法且时间不超过 k k k个月的草加入队列, 直到队列为空
再使用一个二维数组标记草的位置为 1 1 1,其他位置为 0 0 0,最后按题意输出即可
代码:
#include<bits/stdc++.h> using namespace std; #define ll long long const ll MAX = 1e5 + 5; int N, M, k; typedef struct A { int x; int y; int sum; }; queue<A> q; int Map[1001][1001] = {0}; void bfs(A end) { if (Map[end.x][end.y] == 0) { Map[end.x][end.y] = 1; } else { return; } int mark[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; for (int i = 0; i < 4; i++) { A t; t.x = end.x + mark[i][0]; t.y = end.y + mark[i][1]; t.sum = end.sum + 1; if (t.x > 0 && t.x <= N && t.y > 0 && t.y <= M && Map[t.x][t.y] == 0) { q.push(t); } } } int main() { scanf("%d %d", &N, &M); getchar(); for (int i = 1; i <= N; i++) { for (int o = 1; o <= M; o++) { char c = getchar(); if (c == 'g') { A t; t.x = i; t.y = o; t.sum = 0; q.push(t); } } getchar(); } scanf("%d", &k); while (q.size()) { A t = q.front(); q.pop(); if (t.sum > k) break; bfs(t); } for (int i = 1; i <= N; i++) { for (int o = 1; o <= M; o++) { if (Map[i][o]) { printf("g"); } else { printf("."); } } puts(""); } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。