AtCoder ABC 355 C(详细题解及AC代码)

Problem Statement

There is an N*N grid, where the cell at the i-th row from the top and the j-th column from the left contains the integer N*(i-1)+j

Over T turns, integers will be announced. On Turn i the integer Ai is announced, and the cell containing Ai​ is marked. Determine the turn on which Bingo is achieved for the first time. If Bingo is not achieved within T turns, print -1.

Here, achieving Bingo means satisfying at least one of the following conditions:

  • There exists a row in which all N cells are marked.
  • There exists a column in which all N cells are marked.
  • There exists a diagonal line (from top-left to bottom-right or from top-right to bottom-left) in which all N cells are marked.

思路

我们可以设两个数组与两个变量,来分别累加打过标记的A[i,j],其实公式N*(i-1)+j 的本质就是二维转一维,所以不用初始赋值。

假设用row代表行上的累加和,用column代表列上的累加和,用d1代表主对角线,用d2代表副对角线,只要四个当中有一个为N,则就Bingo

代码

Cpp

//https://atcoder.jp/users/XUZIBO
#include <bits/stdc++.h>
using namespace std;
int n,t;
int row[2005],column[2005];
int d1,d2;
bool judge(){
	for(int i=1;i<=n;i++){
		if(row[i]==n||column[i]==n){
			return true;
		}
	}
	if(d1==n||d2==n){
		return true;
	}
	return false;
}
int main(){
	cin>>n;
	cin>>t;
	int pos;
	int i=1;
	while(i<=t){
		cin>>pos;
		int x,y;
		x=(pos-1)/n+1;
		y=(pos-1)%n+1;
		row[x]++;
		column[y]++;
		if(x==y){
			d1++;
		}
		if(x+y==n+1){
			d2++;
		}
		if(judge()){
			cout<<i;
			return 0;
		}
		i++;
	}
	cout<<"-1";
	return 0;
}

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值