中石油训练赛 - Spiral Matrix(找规律)

题目链接:点击查看

题目大意:给出一个 n * m 的矩阵,起点及初始方向可以任意选择,每次可以向前或向右走一步,问有多少种方案可以遍历所有 n * m 个格子

题目分析:找规律的题。。但被数据范围误导了,以为是 dp

手玩一下比较小的样例不难发现,每次旋转一定以直走开始,第一次的直走一定从一条边开始走到相对的另一条边,否则无论如何也无法遍历所有格子,而且如果有方案的话,初始的这条边确定后,答案一定是唯一的

同理将上述所有的有效方案翻转一下方向,这些方案仍然有效,所以答案就是格子之间有多少条直线,然后乘以二,也就是 2 * ( m + n - 2 ),对于 n == 1 和 m == 1 时特判一下就好了

借一下图:https://martin20150405.github.io/2019/12/17/ccpc-2019-final-ti-jie/

代码:
 

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;
     
typedef long long LL;
     
typedef unsigned long long ull;
     
const int inf=0x3f3f3f3f;
   
const int N=2e5+100;

int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
	int w;
	cin>>w;
	int kase=0;
	while(w--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		printf("Case #%d: ",++kase);
		if(n==1&&m==1)
			puts("1");
		else if(n==1||m==1)
			puts("2");
		else
			printf("%d\n",2*(n+m-2));
	}

















    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/109045577
今日推荐