今日SGU 5.18
阿新 • • 發佈:2018-05-18
hide push solution its space 當前 eof end include
SGU 125
題意:給你一個數組b[i][j],表示i,j的四周有多少個數字大於它的,問你能不能構造出一個a矩形
收獲:dfs + 剪枝
一行一行的dfs,然後第一行去枚舉0-9,下一行判斷當前選擇能否滿足上一行對應列的情況,可以的話就繼續dfs
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #defineView Coderepd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #definepdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; constint maxn = 1e5+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll 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; } //inv[1]=1; //for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; int a[6][6],n,b[6][6]; int dx[] = {0,0,1,-1}; int dy[] = {1,-1,0,0}; bool ok(int x,int y){ int ret = 0; // dd(x)dd(y)de(ret) rep(i,0,4) if(a[x+dx[i]][y+dy[i]] > a[x][y]) ret++; return ret==b[x][y]; } bool dfs(int x,int y){ // dd(n)dd(x)de(y) int tx = x,ty = y + 1; if(x > n){ rep(i,1,n+1) if(!ok(n,i)) return false; rep(i,1,n+1) rep(j,1,n+1) printf("%d%c",a[i][j]," \n"[j==n]); return true; } if(ty > n) ty = 1,tx = x + 1; rep(i,0,10){ a[x][y] = i; if(x != 1) if(!ok(x-1,y)) continue; if(dfs(tx,ty)) return true; } return false; } int main(){ scanf("%d",&n); rep(i,1,n+1) rep(j,1,n+1) scanf("%d",&b[i][j]); if(!dfs(1,1)) puts("NO SOLUTION"); return 0; }
今日SGU 5.18