QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#131627#4681. KeyboardingPetroTarnavskyi#TL 660ms377608kbC++172.5kb2023-07-27 19:29:472023-07-27 19:29:48

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-27 19:29:48]
  • 评测
  • 测评结果:TL
  • 用时:660ms
  • 内存:377608kb
  • [2023-07-27 19:29:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FILL(a, b) memset(a, b, sizeof(a))

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int N = 74;
const int MAX = 17474;
const int INF = 1e9 + 47;

int dp[MAX][N][N];
int mn[4][N][N];
char c[N][N];

PII moves[N][N][4];

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

bool ok(int x){
	return 0 <= x && x < N;
}

void prec(){
	FOR(i, 0, N){
		FOR(j, 0, N){
			FOR(t, 0, 4){
				int ni = i, nj = j;
				while(ok(ni) && ok(nj) && c[i][j] == c[ni][nj]){
					ni += dx[t];
					nj += dy[t];
				}
				if(ok(ni) && ok(nj))
					moves[i][j][t] = MP(ni, nj);
				else
					moves[i][j][t] = MP(-1, -1);
			}
		}
	}
}

void calc(int n, int m, int idx){
	set<pair<int, int>> q;
	FOR(i, 0, n){
		FOR(j, 0, m){
			dp[idx][i][j] = dp[idx - 1][i][j];
			q.insert(MP(dp[idx][i][j], i * N + j));
		}
	}
	while(SZ(q)){
		auto [dist, i] = *q.begin();
		q.erase(q.begin());
		
		int x = i / N;
		int y = i % N;
		
		FOR(t, 0, 4){
			if(moves[x][y][t] == MP(-1, -1))
				continue;
			auto [nx, ny] = moves[x][y][t];
			if(nx >= n || ny >= m)
				continue;
			if(dp[idx][nx][ny] > dp[idx][x][y] + 1){
				q.erase(MP(dp[idx][nx][ny], nx * N + ny));
				
				dp[idx][nx][ny] = dp[idx][x][y] + 1;
				
				q.insert(MP(dp[idx][nx][ny], nx * N + ny));				
			}
		}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	FOR (i, 0, N) FOR (j, 0, N) c[i][j] = 'a';
	
	int n, m;
	cin >> n >> m;
	FOR (i, 0, n) FOR (j, 0, m) cin >> c[i][j];
	
	prec();
/*	FOR(i, 0, n){
		FOR(j, 0, m){
			FOR(t, 0, 4)
				cout << moves[i][j][t].F << " " << moves[i][j][t].S << "\n";
			cout << "\n";
		}
		
	}*/
	
	FOR (t, 0, MAX) FOR (i, 0, N) FOR (j, 0, N) dp[t][i][j] = INF;
	dp[0][0][0] = 0;
	
	string s;
	cin >> s;
	s += '*';
	FOR (step, 0, SZ(s))
	{
		calc(n, m, step + 1);
				
		FOR (i, 0, N)
		{
			FOR (j, 0, N)
			{
				if (c[i][j] != s[step])
					dp[step + 1][i][j] = INF;
				//cout << dp[step + 1][i][j] << " ";
			}
		//	cout << endl;
		}
	}
	int ans = INF;
	FOR (i, 0, n) FOR (j, 0, m) ans = min(ans, dp[SZ(s)][i][j]);
	cout << ans + SZ(s) << '\n';
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 27ms
memory: 377460kb

input:

4 7
ABCDEFG
HIJKLMN
OPQRSTU
VWXYZ**
CONTEST

output:

30

result:

ok 1 number(s): "30"

Test #2:

score: 0
Accepted
time: 17ms
memory: 377440kb

input:

5 20
12233445566778899000
QQWWEERRTTYYUUIIOOPP
-AASSDDFFGGHHJJKKLL*
--ZZXXCCVVBBNNMM--**
--------------------
ACM-ICPC-WORLD-FINALS-2015

output:

160

result:

ok 1 number(s): "160"

Test #3:

score: 0
Accepted
time: 16ms
memory: 377456kb

input:

2 19
ABCDEFGHIJKLMNOPQZY
X*****************Y
AZAZ

output:

19

result:

ok 1 number(s): "19"

Test #4:

score: 0
Accepted
time: 12ms
memory: 377392kb

input:

6 4
AXYB
BBBB
KLMB
OPQB
DEFB
GHI*
AB

output:

7

result:

ok 1 number(s): "7"

Test #5:

score: 0
Accepted
time: 28ms
memory: 377488kb

input:

4 3
XYZ
AAA
CAD
B**
A

output:

5

result:

ok 1 number(s): "5"

Test #6:

score: 0
Accepted
time: 3ms
memory: 377412kb

input:

1 2
A*
A

output:

3

result:

ok 1 number(s): "3"

Test #7:

score: 0
Accepted
time: 28ms
memory: 377472kb

input:

2 1
*
A
A

output:

4

result:

ok 1 number(s): "4"

Test #8:

score: 0
Accepted
time: 23ms
memory: 377476kb

input:

12 11
AAAAAAAAAAA
ABBBBBBBBBA
ABCCCCCCCBA
ABCDDDDDCBA
ABCDEEEDCBA
ABCDEFEDCBA
ABCDEEEDCBA
ABCDDDDDCBA
ABCCCCCCCBA
ABBBBBBBBBA
AAAAAAAAAAA
***********
AAA

output:

5

result:

ok 1 number(s): "5"

Test #9:

score: 0
Accepted
time: 386ms
memory: 377608kb

input:

6 28
AABBCCDDEEFFGGHHIIJJKKLLMNNN
AABBCCDDEEFFGGHHIIJJKKLLMMMN
OOPPQQRRSSTTUUVVWWXXYYZZ00**
OOPPQQRRSSTTUUVVWWXXYYZZ0011
222333444555666777888999--11
222333444555666777888999----
1THE-QUICK-BROWN-FOX-JUMPS-OVER-THE-LAZY-DOG2THE-QUICK-BROWN-FOX-JUMPS-OVER-THE-LAZY-DOG3THE-QUICK-BROWN-FOX-JUMPS-OVER-T...

output:

64174

result:

ok 1 number(s): "64174"

Test #10:

score: 0
Accepted
time: 660ms
memory: 377524kb

input:

30 30
A11111111111111111111111111111
0B1111111111111111111111111111
00C111111111111111111111111111
000D11111111111111111111111111
0000E1111111111111111111111111
00000F111111111111111111111111
000000G11111111111111111111111
0000000H1111111111111111111111
00000000I111111111111111111111
000000000J11111...

output:

590057

result:

ok 1 number(s): "590057"

Test #11:

score: 0
Accepted
time: 612ms
memory: 377540kb

input:

30 30
A11111111111111111111111111111
0B1111111111111111111111111111
00C111111111111111111111111111
000D11111111111111111111111111
0000E1111111111111111111111111
00000F111111111111111111111111
000000G11111111111111111111111
0000000H1111111111111111111111
00000000I111111111111111111111
000000000J11111...

output:

30001

result:

ok 1 number(s): "30001"

Test #12:

score: 0
Accepted
time: 33ms
memory: 377508kb

input:

4 10
QWERTYUIOP
ASDFGHJKL*
ZXCVBNM***
----------
GA-NU-MAAR-LIGGEN-LIEFSTE-IN-DE-TUIN-DE-LEGE-PLEKKEN-IN-HET-HOGE-GRAS-IK-HEB-ALTIJD-GEWILD-DAT-IK-DAT-WAS-EEN-LEGE-PLEK-VOOR-IEMAND-OM-TE-BLIJVEN

output:

676

result:

ok 1 number(s): "676"

Test #13:

score: 0
Accepted
time: 57ms
memory: 377464kb

input:

24 30
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
QQQWWWEEERRRTTTYYYUUUIIIOOOPPP
AAASSSDDDFFFGGGHHHJJJKKKLLL***
AAASSSDDDFFFGGGHHHJJJKKKLLL***
AAASSSDDDFFFGGGHHHJJJKKKLLL***
AAASSSDDDFFFGGG...

output:

2236

result:

ok 1 number(s): "2236"

Test #14:

score: -100
Time Limit Exceeded

input:

50 50
*0000000000000000000000000000000000000000000000000
00011001100110011001100110011001100110011001100111
01001100110011001100110011001100110011001100110011
01100110011001100110011001100110011001100110011001
00110011001100110011001100110011001100110011001101
000110011001100110011001100110011001100...

output:


result: