QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#672717#4434. LemurskircoTL 749ms8668kbC++233.9kb2024-10-24 18:24:592024-10-24 18:25:00

Judging History

你现在查看的是最新测评结果

  • [2024-10-24 18:25:00]
  • 评测
  • 测评结果:TL
  • 用时:749ms
  • 内存:8668kb
  • [2024-10-24 18:24:59]
  • 提交

answer

#include <bits/stdc++.h>
#define iosy ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1005;
int dx[4]={1,0,-1,0},dy[4]={0,-1,0,1};
int n,m,k;
vector<vector<char>> mp(N,vector<char>(N));
bool vis1[N][N],vis2[N][N],use[N][N];
char ans[N][N];

bool pd(int x,int y){//判断是否出图
	if(x<1||x>n||y<1||y>m)return false;
	return true;
}

void bfs1(int a, int b) {
    queue<pair<int,int>> q;
    q.push({a, b});
    vis1[a][b] = 1;

    while(!q.empty()) {
        auto& [x, y] = q.front();
        q.pop();

        if (mp[x][y] == '.') {
            // 使用分层 BFS 来扩展曼哈顿距离 <= k 的区域
            queue<pair<int,int>> layer;
            layer.push({x, y});
            use[x][y] = 1;

            vector<vector<bool>> visLayer(n + 1, vector<bool>(m + 1, false));
            visLayer[x][y] = true;
            int dist = 0;

            while (!layer.empty() && dist <= k) {
                int size = layer.size();
                for (int i = 0; i < size; i++) {
                    auto [lx, ly] = layer.front();
                    layer.pop();

                    for (int j = 0; j < 4; j++) {
                        int nx = lx + dx[j], ny = ly + dy[j];
                        if (pd(nx, ny) && !visLayer[nx][ny] && abs(nx - x) + abs(ny - y) <= k) {
                            visLayer[nx][ny] = true;
                            use[nx][ny] = 1;
                            layer.push({nx, ny});
                        }
                    }
                }
                dist++;
            }
        }

        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (pd(nx, ny) && !vis1[nx][ny]) {
                vis1[nx][ny] = 1;
                q.push({nx, ny});
            }
        }
    }
}

void bfs2(int a, int b) {
    queue<pair<int,int>> q;
    q.push({a, b});
    vis2[a][b] = 1;

    while(!q.empty()) {
        auto& [x, y] = q.front();
        q.pop();

        if (!use[x][y]) {
            // 分层 BFS 扩展觅食范围
            queue<pair<int,int>> layer;
            layer.push({x, y});
            ans[x][y] = 'x';

            vector<vector<bool>> visLayer(n + 1, vector<bool>(m + 1, false));
            visLayer[x][y] = true;
            int dist = 0;

            while (!layer.empty() && dist <= k) {
                int size = layer.size();
                for (int i = 0; i < size; i++) {
                    auto [lx, ly] = layer.front();
                    layer.pop();

                    for (int j = 0; j < 4; j++) {
                        int nx = lx + dx[j], ny = ly + dy[j];
                        if (pd(nx, ny) && !visLayer[nx][ny] && abs(nx - x) + abs(ny - y) <= k) {
                            visLayer[nx][ny] = true;
                            ans[nx][ny] = 'x';
                            layer.push({nx, ny});
                        }
                    }
                }
                dist++;
            }
        }

        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (pd(nx, ny) && !vis2[nx][ny]) {
                vis2[nx][ny] = 1;
                q.push({nx, ny});
            }
        }
    }
}

void solve(){
	cin>>n>>m>>k;//觅食范围是一个菱形
	for(int i=1;i<=n;i++){
		string s;cin>>s;s=" "+s;
		for(int j=1;j<=m;j++){
			mp[i][j]=s[j];
		}
	}
	bfs1(1,1);
	bfs2(1,1);
	bool ok=1;//假设可以
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(mp[i][j]=='x'&&ans[i][j]!='x'){
				ok=0;
				break;
			}
		}
	}
	cout<<(ok? "TAK\n":"NIE\n");
	//清空多测
	memset(vis1,0,sizeof(vis1));
	memset(vis2,0,sizeof(vis2));
	memset(use,0,sizeof(use));
	memset(ans,'B',sizeof(ans));
}

signed main()
{
	iosy;
	int _t=1;
	cin>>_t;
	while(_t--){
		solve();
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 749ms
memory: 8668kb

input:

4000
1 1 1
.
1 1 1
x
1 1 1000
.
1 1 1000
x
1 1000 4
..........................................xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx....xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:

TAK
TAK
TAK
TAK
TAK
NIE
NIE
TAK
NIE
TAK
NIE
NIE
TAK
TAK
NIE
TAK
TAK
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
TAK
NIE
NIE
TAK
TAK
TAK
NIE
NIE
NIE
TAK
TAK
NIE
NIE
TAK
NIE
NIE
NIE
TAK
NIE
NIE
NIE
TAK
NIE
NIE
NIE
NIE
TAK
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
TAK
NIE
NIE
NIE
TAK
TAK
...

result:

ok 4000 lines

Test #2:

score: -100
Time Limit Exceeded

input:

36
1000 1000 2
....xxxx..............xxxxxx..........xx..xxxx......xxxxxxxxxxx.........xxxxxxxxxx...xxxxxx....xxxxxx.......xxxxxx....xxxxxx......................................xx.............xxxxxxxxx......xxxxxxx................xxxxxx..xxxxxx....xxxxxx..............xxxxxxxxxxxxxxxxxxxxxxxxxxxx...x...

output:


result: