Hướng dẫn giải của NKTable
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của ladpro98
program NKTABLE; uses math; const maxn=555; fi=''; dx:array[1..2] of longint = (0,1); dy:array[1..2] of longint = (1,0); type e=record x,y:longint; end; var a:array[1..maxn,1..maxn] of longint; q:array[1..2*maxn*maxn] of e; res:array[1..maxn+maxn] of longint; chk,chk2:array[1..maxn,1..maxn] of boolean; m,n,l,r,layer:longint; procedure input; var inp:text; i,j:longint; begin assign(inp,fi);reset(inp); readln(inp,m,n); for i:=1 to m do for j:=1 to n do read(inp,a[i,j]); close(inp); end; procedure bfsChk; var l,r,i,x,y:longint; u:e; begin l:=1;r:=1; q[1].x:=m;q[1].y:=n; chk[m,n]:=true; while l<=r do begin u:=q[l];inc(l); for i:=1 to 2 do begin x:=u.x-dx[i]; y:=u.y-dy[i]; if (0<x) and (0<y) and (x<=m) and (y<=n) and (not chk[x,y]) and (a[x,y]<2) then begin inc(r); q[r].x:=x; q[r].y:=y; chk[x,y]:=true; end; end; end; end; function bfs:boolean; var i,j,x,y:longint; u:e; found:boolean; begin j:=r; found:=false; while l<=r do begin u:=q[l];inc(l); for i:=1 to 2 do begin x:=u.x+dx[i]; y:=u.y+dy[i]; if (0<x) and (0<y) and (x<=m) and (y<=n) and (chk[x,y]) and (not chk2[x,y]) and (a[x,y]<2) then begin if a[x,y] = 1 then found:=true; chk2[x,y]:=true; inc(j); q[j].x:=x; q[j].y:=y; end; end; end; r:=j; exit(found); end; procedure resetQueue; var tl,tr,i:longint; begin tl:=l;tr:=r; l:=1;r:=0; for i:=tl to tr do if a[q[i].x,q[i].y]=1 then begin inc(r); q[r]:=q[i]; end; end; begin input; bfsChk; l:=1;r:=1;q[1].x:=1;q[1].y:=1; res[1]:=a[1,1]; for layer:=2 to m+n-1 do begin if bfs then begin res[layer]:=1; resetQueue; end else res[layer]:=0; end; for layer:=1 to m+n-1 do write(res[layer]); end.
Code mẫu của RR
#include <sstream> #include <iomanip> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <string> #include <deque> #include <complex> #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++) #define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--) #define SET(a,v) memset(a,v,sizeof(a)) #define sqr(x) ((x)*(x)) #define ll long long #define F first #define S second #define PB push_back #define MP make_pair #define DEBUG(x) cout << #x << " = "; cout << x << endl; #define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; #define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; using namespace std; //Buffer reading int INP,AM,REACHEOF; #define BUFSIZE (1<<12) char BUF[BUFSIZE+1], *inp=BUF; #define GETCHAR(INP) { \ if(!*inp) { \ if (REACHEOF) return 0;\ memset(BUF,0,sizeof BUF);\ int inpzzz = fread(BUF,1,BUFSIZE,stdin);\ if (inpzzz != BUFSIZE) REACHEOF = true;\ inp=BUF; \ } \ INP=*inp++; \ } #define DIG(a) (((a)>='0')&&((a)<='9')) #define GN(j) { \ AM=0;\ GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\ if (INP=='-') {AM=1;GETCHAR(INP);} \ j=INP-'0'; GETCHAR(INP); \ while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \ if (AM) j=-j;\ } //End of buffer reading const long double PI = acos((long double) -1.0); typedef pair<int,int> P; const int di[] = {0,1}; const int dj[] = {1,0}; int m, n, a[511][511]; P tr[511][511]; queue< P > cur, next[2]; deque< queue<P> > v; bool inside(int u, int v) { return (u > 0) && (u <= m) && (v > 0) && (v <= n); } void clear(queue< P > &q) { while (!q.empty()) q.pop(); } void trace(int m, int n) { if (m == 1 && n == 1) { printf("%d", a[1][1]); return ; } trace(tr[m][n].F, tr[m][n].S); printf("%d", a[m][n]); } int main() { scanf("%d%d", &m, &n); FOR(i,1,m) FOR(j,1,n) scanf("%d", &a[i][j]); clear(cur); cur.push(MP(1,1)); v.push_back(cur); while (!v.empty()) { cur = v.front(); clear(next[0]); clear(next[1]); while (!cur.empty()) { P u = cur.front(); cur.pop(); REP(dir,2) { P v = MP(u.F + di[dir], u.S + dj[dir]); if (inside(v.F, v.S) && a[v.F][v.S] < 2 && tr[v.F][v.S] == MP(0,0)) { tr[v.F][v.S] = u; next[a[v.F][v.S]].push(v); } } } if (!next[1].empty()) v.push_back(next[1]); if (!next[0].empty()) v.push_back(next[0]); v.pop_front(); } trace(m, n); puts(""); return 0; }
Code mẫu của hieult
#include <set> #include <map> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <cassert> #include <cstdlib> #include <cstring> #include <sstream> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int ui; typedef unsigned long long ull; #define Rep(i,n) for(typeof(n) i = 0; i < (n); ++i) #define Repd(i,n) for(typeof(n) i = (n)-1; i >= 0; --i) #define For(i,a,b) for(typeof(b) i = (a); i <= (b); ++i) #define Ford(i,a,b) for(typeof(a) i = (a); i >= (b); --i) #define Fit(i,v) for(typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i) #define Fitd(i,v) for(typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i) #define mp make_pair #define pb push_back #define fi first #define se second #define sz(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() #define ms(a,x) memset(a, x, sizeof(a)) #define nl puts("") #define sp printf(" ") #define ok puts("ok") //#include <conio.h> template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template<class T> void db(T a, int p = -1) { if (p >= 0) cout << fixed << setprecision(p); cout << a << " "; } template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T sqr(T x) { return x * x; } template<class T> T cube(T x) { return x * x * x; } template<class T> struct Triple { T x, y, z; Triple() {} Triple(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {} }; template<class T> Triple<T> euclid(T a, T b) { if (b == 0) return Triple<T>(1, 0, a); Triple<T> r = euclid(b, a % b); return Triple<T>(r.y, r.x - a / b * r.y, r.z); } template<class T> int getbit(T s, int i) { return (s >> i) & 1; } template<class T> T onbit(T s, int i) { return s | (T(1) << i); } template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0;int ptr = 0; char gc() { if (rsz <= 0) { ptr = 0; rsz = fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; } void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); } int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; } template<class T> bool gi(T &v) { v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc(); while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true; } const double PI = 2 * acos(0); const string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int dr[] = {0, +1}; const int dc[] = {+1, 0}; const int inf = (int)1e9 + 5; const ll linf = (ll)1e16 + 5; const double eps = 1e-9; const ll mod = 100000007; typedef pair<int, int> II; #define maxn 505 #define maxe 100005 int n, m, a[maxn][maxn]; int test; bool b[maxn][maxn], g[maxn][maxn]; bool thoaman(int r, int c){ return (r > 0 && r <= n && c > 0 && c <= m && a[r][c] != 2); } void dfs(int r, int c){ // printf("%d %d\n", r, c); int rr, cc; Rep(t, 2){ rr = r - dr[t]; cc = c - dc[t]; if(thoaman(rr, cc) && ! b[rr][cc]){ b[rr][cc] = 1; dfs(rr, cc); } } } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); ms(b, 0); ms(g, 0); scanf("%d %d", &n, &m); For(i, 1, n) For(j, 1, m) scanf("%d", &a[i][j]); b[n][m] = 1; dfs(n, m); vector<int> res; res.pb(a[1][1]); g[1][1] = 1; int j, r, c; For(add, 2, n + m - 1){ int MAX = 0; queue<II> q; for(int i = 1; i <= n && i < add; i++){ j = add - i; if(thoaman(i, j) && g[i][j]){ // printf("%d %d\n", i, j); Rep(t, 2){ r = i + dr[t]; c = j + dc[t]; if(thoaman(r, c) && b[r][c]){ MAX = max(MAX, a[r][c]); q.push(mp(r, c)); } } } } res.pb(MAX); while(!q.empty()){ r = q.front().fi; c = q.front().se; q.pop(); if(a[r][c] == MAX) g[r][c] = 1; } } Rep(i, sz(res)) printf("%d", res[i]); // cout << clock() << endl; return 0; }
Code mẫu của skyvn97
#include<bits/stdc++.h> #define MAX 505 struct pos { int x,y; pos(){} pos(const int &_x,const int &_y) { x=_x;y=_y; } }; int a[MAX][MAX]; bool mv[MAX][MAX]; bool vs[MAX][MAX]; int m,n; int res[2*MAX]; pos v[2][2*MAX]; int sz[2]; void init(void) { scanf("%d",&m); scanf("%d",&n); int i,j; for (i=1;i<=m;i=i+1) for (j=1;j<=n;j=j+1) scanf("%d",&a[i][j]); } void optimize(void) { int i,j; mv[m][n]=true; vs[m][n]=false; for (i=m;i>=1;i=i-1) for (j=n-(i==m);j>=1;j=j-1) { if (a[i][j]==2) mv[i][j]=false; else mv[i][j]=mv[i+1][j]||mv[i][j+1]; vs[i][j]=false; } for (i=0;i<=m+1;i=i+1) { mv[i][0]=false; mv[i][n+1]=false; } for (i=0;i<=n+1;i=i+1) { mv[0][i]=false; mv[m+1][i]=false; } } void BFS(void) { int st,i,t,mval; sz[1]=1; v[1][1]=pos(1,1); res[1]=a[1][1]; for (st=1;st<m+n-1;st=st+1) { t=st%2; mval=0; for (i=1;i<=sz[t];i=i+1) { if (mv[v[t][i].x+1][v[t][i].y] && mval<a[v[t][i].x+1][v[t][i].y]) mval=a[v[t][i].x+1][v[t][i].y]; if (mv[v[t][i].x][v[t][i].y+1] && mval<a[v[t][i].x][v[t][i].y+1]) mval=a[v[t][i].x][v[t][i].y+1]; } res[st+1]=mval; sz[1-t]=0; for (i=1;i<=sz[t];i=i+1) { if (!vs[v[t][i].x+1][v[t][i].y] && mv[v[t][i].x+1][v[t][i].y] && mval==a[v[t][i].x+1][v[t][i].y]) { sz[1-t]++; v[1-t][sz[1-t]]=pos(v[t][i].x+1,v[t][i].y); vs[v[t][i].x+1][v[t][i].y]=true; } if (!vs[v[t][i].x][v[t][i].y+1] && mv[v[t][i].x][v[t][i].y+1] && mval==a[v[t][i].x][v[t][i].y+1]) { sz[1-t]++; v[1-t][sz[1-t]]=pos(v[t][i].x,v[t][i].y+1); vs[v[t][i].x][v[t][i].y+1]=true; } } } for (st=1;st<m+n;st=st+1) printf("%d",res[st]); } int main(void) { init(); optimize(); BFS(); return 0; }
Bình luận