QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#39675#2931. Tri-Color PuzzlenidhsTL 15ms3668kbC++1.6kb2022-07-12 18:54:462022-07-12 18:54:47

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-07-12 18:54:47]
  • 评测
  • 测评结果:TL
  • 用时:15ms
  • 内存:3668kb
  • [2022-07-12 18:54:46]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define mod 1000000007
#define pb push_back
typedef pair<int,int> pir;
string sstr[]={"NO\n","YES\n"};
const int N=30;
class node
{
public:
	int lay;
	int st;
};
int T,n,m;
int g[N][N];
int p3[N];
queue<node> q;
int ans=0;
//
bool check(int a,int b,int c)
{
	if(a==b&&b==c) return 1;
	if(a!=b&&b!=c&&a!=c) return 1;
	return 0;
}
signed main()
{
	p3[0]=1;
	for(int i=1;i<=20;i++) p3[i]=p3[i-1]*3;
	cin>>n>>m;
	memset(g,-1,sizeof(g));
	for(int i=1;i<=m;i++){
		int x,y,c;
		cin>>x>>y>>c;
		g[x][y-1]=c;
	}
	if(g[1][0]==-1){
		q.push(node{1,0});
		q.push(node{1,1});
		q.push(node{1,2});
	}
	else q.push(node{1,g[1][0]});
	while(!q.empty()){
		auto tmp=q.front();
		int lay=tmp.lay;
		//cout<<"|||"<<lay<<' '<<tmp.st<<'\n';
		q.pop();
		if(tmp.lay==n){
			ans++;
			continue;
		}
		for(int i=0;i<3;i++){//枚举第一个
			if(g[lay+1][0]!=-1&&g[lay+1][0]!=i) continue;
			int cur=i;//当前数值
			int pre=i;
			int cst=i;//当前状态
			bool flag=1;
			//cout<<"---"<<i<<'\n';
			for(int j=1;j<=lay;j++){
				int up=tmp.st/p3[j-1]%3;
				if(up==pre) cur=pre;
				else if(up!=0&&pre!=0) cur=0;
				else if(up!=1&&pre!=1) cur=1;
				else cur=2;
				//cout<<"+++"<<i<<' '<<j<<' '<<cur<<' '<<'\n';
				if(g[lay+1][j]!=-1&&g[lay+1][j]!=cur){
					flag=0;
					break;
				}
				//cout<<"***"<<lay<<' '<<tmp.st<<' '<<i<<' '<<j<<' '<<cur<<'\n';
				pre=cur;
				cst+=cur*p3[j];
			}
			if(flag) q.push(node{lay+1,cst});
		}

	}
	cout<<ans<<'\n';

}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3584kb

input:

4 4
1 1 0
2 1 2
4 1 2
4 4 2

output:

0

result:

ok single line: '0'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3460kb

input:

4 4
1 1 1
2 1 2
4 1 2
3 3 2

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3580kb

input:

4 4
1 1 0
2 1 1
4 1 0
4 4 0

output:

3

result:

ok single line: '3'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3660kb

input:

6 4
1 1 0
2 2 0
5 1 1
5 5 2

output:

9

result:

ok single line: '9'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3508kb

input:

17 16
3 2 2
4 4 0
5 2 1
6 4 1
8 5 2
8 8 2
11 1 2
11 7 0
12 4 2
12 10 2
12 11 0
13 11 2
14 5 2
14 8 0
16 7 0
17 5 1

output:

3

result:

ok single line: '3'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3636kb

input:

11 9
2 2 2
5 3 0
6 5 2
9 7 1
10 10 1
11 4 1
11 7 0
11 10 1
11 11 2

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3560kb

input:

4 4
2 1 1
4 1 2
4 3 1
4 4 2

output:

1

result:

ok single line: '1'

Test #8:

score: 0
Accepted
time: 15ms
memory: 3668kb

input:

16 7
6 3 0
9 5 0
11 11 2
12 7 1
13 10 2
14 7 0
15 10 1

output:

19683

result:

ok single line: '19683'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3516kb

input:

10 4
4 3 2
8 2 2
8 6 1
9 5 0

output:

729

result:

ok single line: '729'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3448kb

input:

18 29
4 1 2
6 5 0
7 3 2
7 5 0
8 6 1
9 1 0
9 6 1
10 1 0
10 2 0
10 3 0
10 6 1
11 2 2
11 8 2
11 11 0
13 8 0
14 3 1
14 5 1
14 6 1
14 9 0
14 12 0
15 4 1
15 7 1
15 11 1
15 14 2
16 8 1
16 9 0
16 15 2
18 6 1
18 16 2

output:

1

result:

ok single line: '1'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3580kb

input:

5 5
3 2 0
3 3 1
4 1 1
5 1 0
5 3 0

output:

1

result:

ok single line: '1'

Test #12:

score: 0
Accepted
time: 2ms
memory: 3604kb

input:

11 8
4 3 0
6 3 1
6 5 0
6 6 1
7 3 2
10 3 0
10 7 0
11 11 2

output:

27

result:

ok single line: '27'

Test #13:

score: 0
Accepted
time: 1ms
memory: 3464kb

input:

6 6
2 2 1
3 1 0
3 3 1
5 5 1
6 1 1
6 5 1

output:

1

result:

ok single line: '1'

Test #14:

score: 0
Accepted
time: 2ms
memory: 3448kb

input:

11 7
3 2 0
3 3 2
5 1 0
7 3 2
10 1 1
10 6 2
10 8 0

output:

81

result:

ok single line: '81'

Test #15:

score: 0
Accepted
time: 2ms
memory: 3644kb

input:

8 7
3 1 2
4 2 1
5 2 0
5 3 1
5 4 0
6 3 2
8 8 2

output:

0

result:

ok single line: '0'

Test #16:

score: -100
Time Limit Exceeded

input:

19 5
9 7 0
11 10 0
18 9 0
19 10 1
19 17 0

output:


result: