提交时间:2022-10-04 11:26:35

运行 ID: 57464

#include <bits/stdc++.h> #define mod ((unsigned long long)(1<<64)) #define B ((unsigned long long)(1e15)) #define C ((unsigned long long)(2e15+21)) #define ll long long #define ull unsigned long long using namespace std; ull ans; char d[1005][1005]; int n, m, v[1005][1005]; struct node { bool vis=false; short lock=0; ll mx=0, mn=0; } f[1005][1005]; inline bool check(int x, int y) { if(x<1 || x>n) return false; else if(y<1 || y>m) return false; if(f[x][y].lock == 2) return false; return true; } ull MI(ull basc, int cf) { ull ret = 1; while(cf) { if(cf&1) ret *= basc; basc *= basc; cf >>= 1; } return ret; } node ssearch(int x, int y) { if(f[x][y].vis) return f[x][y]; node ret, tot; f[x][y].lock++; tot.mx = v[x][y]; tot.mn = -v[x][y]; int xx=x, yy=y; if(d[x][y] == '>') y += 1; else if(d[x][y] == '<') y -= 1; else if(d[x][y] == '^') x -= 1; else x += 1; if(check(x, y)) ret = ssearch(x, y); if(ret.mn > 0) tot.mx += ret.mn; if(ret.mx < 0) tot.mn += ret.mx; f[xx][yy].lock--; return f[xx][yy] = tot; } int main() { scanf("%d%d", &n, &m); for(int i=1; i<=n; i++) scanf("%s", d[i]+1); for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) scanf("%d", &v[i][j]); } for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { ssearch(i, j); f[i][j].vis = true; } } for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) ans = (ans+(f[i][j].mx+B)*MI(C, (i-1)*m+j-1))%mod; } printf("%ull\n", ans); return 0; }