Submission #1141116

#TimeUsernameProblemLanguageResultExecution timeMemory
1141116SmuggingSpunFurniture (JOI20_furniture)C++20
5 / 100
301 ms836 KiB
#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
int n, m;
namespace sub1{
	void solve(){
		vector<vector<bool>>c(n, vector<bool>(m, false));
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				int x;
				cin >> x;
				if(x == 1){
					c[i][j] = true;
				}
			}
		}
		int q;
		cin >> q;
		for(int _ = 0; _ < q; _++){
			int x, y;
			cin >> x >> y;
			c[--x][--y] = true;
			vector<vector<bool>>vis(n, vector<bool>(m, false));
			auto dfs = [&] (auto&& self, int x, int y){
				if(x < 0 || x == n || y < 0 || y == m || vis[x][y] || c[x][y]){
					return;
				}
				vis[x][y] = true;
				self(self, x + 1, y);
				self(self, x, y + 1);
			};
			dfs(dfs, 0, 0);
			cout << vis[n - 1][m - 1] << "\n";
			if(!vis[n - 1][m - 1]){
				c[x][y] = false;
			}
		}
	}
}
namespace sub2{
	void solve(){
		vector<vector<bool>>init(n, vector<bool>(m, false)), c(n, vector<bool>(m, false));
		vector<vector<pair<int, int>>>point(n, vector<pair<int, int>>(m));
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				point[i][j] = make_pair(i, j);
				int x;
				cin >> x;
				if(x == 1){
					init[i][j] = true;
				}
			}
		}
		for(int i = 0; i + 1 < n; i++){
			point[i][m - 1] = make_pair(i + 1, m - 1); 
		}
		for(int j = 0; j + 1 < m; j++){
			point[n - 1][j] = make_pair(n - 1, j + 1);
		}
		auto move = [&] (int i, int j){
			c[i][j] = true;
			queue<pair<int, int>>q;
			q.emplace(i - 1, j);
			q.emplace(i, j - 1);
			while(!q.empty()){
				auto [x, y] = q.front();
				q.pop();
				if(x < 0 || y < 0 || c[x][y]){
					continue;
				}
				bool right = (y + 1 < m && !c[x][y + 1]), down = (x + 1 < n && !c[x + 1][y]);
				if(right && down){
					if(point[x + 1][y] == point[x][y + 1]){
						point[x][y] = point[x][y + 1];
					}
				}
				else if(right){
					point[x][y] = make_pair(x, y + 1);
				}
				else if(down){
					point[x][y] = make_pair(x + 1, y);
				}
				else{
					c[x][y] = true;
					q.emplace(x - 1, y);
					q.emplace(x, y - 1);
				}
			}	
		};
		for(int i = n - 1; i > -1; i--){
			for(int j = m - 1; j > -1; j--){
				if(init[i][j] && !c[i][j]){
					move(i, j);
				}
			}
		}
//		for(int i = 0; i < n; i++){
//			for(int j = 0; j < m; j++){
//				cout << c[i][j] << " ";
//			}
//			cout << endl;
//		}
		int q;
		cin >> q;
		for(int _ = 0; _ < q; _++){
			int x, y;
			cin >> x >> y;
			if(c[--x][--y]){
				cout << "1\n";
				continue;
			}
			bool flag = true;
			int i = 0, j = 0;
			while(true){
				if(i == n - 1 && j == m - 1){
					break;
				}
				bool right = (j + 1 < m && !c[i][j + 1]), down = (i + 1 < n && !c[i + 1][j]);
				if(right && down){
					if(point[i + 1][j] == point[i][j + 1]){
						point[i][j] = point[i][j + 1];
					}
				}
				else if(right){
					point[i][j] = make_pair(i, j + 1);
				}
				else{
					point[i][j] = make_pair(i + 1, j);
				}
				auto [I, J] = point[i][j];
				if(I == i && J == j){
					break;
				}
				i = I;
				if((j = J) == y && i == x){
					flag = false;
					break;
				}
			}
			if(flag){
				cout << "1\n";
				move(x, y);
			}
			else{
				cout << "0\n";
			}
		}
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
	cin >> n >> m;
	if(n <= 100 && m <= 100){
		sub1::solve();
	}
	else{
		sub2::solve();
	}
}

Compilation message (stderr)

furniture.cpp: In function 'int main()':
furniture.cpp:153:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...