记忆化搜索
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; #define INF 0x7fffffff #define N 710 int n,m; int aa,bb,cc,dd; int xx[4]={0,0,1,-1}, yy[4]={1,-1,0,0}; int a[N][N],f[N][N]; char ch[2]; bool v[N][N]; int read() { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } void change(int x,int y,int val) { a[x][y]=val; } void mark(int aa,int bb,int cc,int dd,bool f) { for (int i=aa;i<=bb;i++) for (int j=cc;j<=dd;j++) v[i][j]=f; } int dfs(int x,int y) { if (v[x][y]) return -INF; if (f[x][y]!=-1) return f[x][y]; f[x][y]=1; for (int i=0;i<4;i++) { int tx=x+xx[i], ty=y+yy[i]; if (tx<1 || ty<1 || tx>n || ty>n) continue; if (a[x][y]>a[tx][ty]) f[x][y]=max(f[x][y],dfs(tx,ty)+1); } return f[x][y]; } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) scanf("%d",&a[i][j]); scanf("%d",&m); while (m--) { scanf("%s",ch); if (ch[0]==‘C‘) { scanf("%d%d%d",&aa,&bb,&cc); change(aa,bb,cc); } if (ch[0]==‘S‘) { scanf("%d%d%d%d",&aa,&cc,&bb,&dd); mark(aa,bb,cc,dd,1); } if (ch[0]==‘B‘) { scanf("%d%d%d%d",&aa,&cc,&bb,&dd); mark(aa,bb,cc,dd,0); } if (ch[0]==‘Q‘) { int maxn=0; memset(f,-1,sizeof(f)); for (int j=1;j<=n;j++) for (int k=1;k<=n;k++) maxn=max(maxn,dfs(j,k)); printf("%d\n",maxn); } } return 0; }
时间: 2024-10-13 00:13:28