Codevs 1160 蛇形矩阵 - 模拟

22 篇文章 0 订阅

只会有四种拐法:左上 上右 右下 下左
每次走的时候判断拐弯条件,如果条件都不满足,就接着上次的方向走。
PS:好难调啊Orz

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define debug(x) cerr << #x << "=" << x << endl;
int g[110][110];
int ansx, ansy;//左上 上右 右下 下左
int n,size = 1,ans;
int check(int x, int y, int ope) {
    int id = ope;
    if(id==1) 
        if(y-1<1||g[x][y-1]) 
            return 4;
    if(id==4) 
        if(x-1<1||g[x-1][y]) 
            return 3;
    if(id==3) 
        if(y+1>n||g[x][y+1]) 
            return 2;
    if(id==2)
        if(x+1>n||g[x+1][y]) 
            return 1;
    return id;
}
int main() {
    cin >> n;
    int x = n;
    int y = n;
    size = n*n;
    int ope = 1;
    while(size >= 1) {
        g[x][y] = size;
        ope = check(x,y,ope);
        if(ope == 1) 
            y--;
        if(ope == 2) 
            x++;
        if(ope == 3) 
            y++;
        if(ope == 4) 
            x--;
        size--;
    }
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=n; j++) 
            cout << g[i][j] << " ";
        cout << endl;
    }
    for(int i=1; i<=n; i++) 
        ans += g[i][i];
    int tem = 1;
    for(int i=n; i>=1; i--) 
        ans += g[i][tem++];
    cout << --ans;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值