Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- struct ph {
- inline size_t operator()(const pair<int,int> & v) const {
- return v.first*53+v.second;
- }
- };
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int n; cin >> n;
- bool bad[n+2][n+2];
- for (int i = 0; i < n+2; i++) {
- bad[0][i] = true;
- bad[n+1][i] = true;
- bad[i][0] = true;
- bad[i][n+1] = true;
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- cin >> bad[i+1][j+1];
- }
- }
- ll dp[n+2][n+2];
- for (int i = 0; i < n+2; i++) {
- for (int j = 0; j < n+2; j++) {
- dp[i][j] = 0;
- }
- }
- vector<pair<int,int>> order;
- unordered_set<pair<int,int>, ph> vis;
- queue<pair<int,int>> bfs;
- bfs.push(make_pair(0,0));
- dp[1][1]=1;
- while (!bfs.empty()) {
- pair<int,int> t = bfs.front();
- bfs.pop();
- if (vis.find(t) != vis.end()) {
- continue;
- }
- if (bad[t.first+1][t.second+1]) {
- continue;
- }
- vis.insert(t);
- order.push_back(t);
- bfs.push(make_pair(t.first+1,t.second));
- bfs.push(make_pair(t.first-1,t.second));
- bfs.push(make_pair(t.first,t.second+1));
- bfs.push(make_pair(t.first,t.second-1));
- if (t.first==n-1 && t.second==n-1) {
- break;
- }
- }
- for (int i = 0; i < order.size(); i++) {
- pair<int, int> t = order[i];
- dp[t.first][t.second+1] += dp[t.first+1][t.second+1];
- dp[t.first+2][t.second+1] += dp[t.first+1][t.second+1];
- dp[t.first+1][t.second] += dp[t.first+1][t.second+1];
- dp[t.first+1][t.second+2] += dp[t.first+1][t.second+1];
- }
- for (int i = 1; i < n+1; i++) {
- for (int j = 1; j < n+1; j++) {
- cout << dp[i][j] << " ";
- }
- cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement