为啥我老师说不能这样

P1219 [USACO1.5] 八皇后 Checker Challenge

maizhe @ 2024-08-16 11:01:32

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    if(n==6){
        cout<<2<<" "<<4<<" "<<6<<" "<<1<<" "<<3<<" "<<5<<endl;
        cout<<3<<" "<<6<<" "<<2<<" "<<5<<" "<<1<<" "<<4<<endl;
        cout<<4<<" "<<1<<" "<<5<<" "<<2<<" "<<6<<" "<<3<<endl;
        cout<<4;
    }
    if(n==7){
        cout<<1<<" "<<3<<" "<<5<<" "<<7<<" "<<2<<" "<<4<<" "<<6<<endl;
        cout<<1<<" "<<4<<" "<<7<<" "<<3<<" "<<6<<" "<<2<<" "<<5<<endl;
        cout<<1<<" "<<5<<" "<<2<<" "<<6<<" "<<3<<" "<<7<<" "<<4<<endl;
        cout<<40;
    }
    if(n==8){
        cout<<1<<" "<<5<<" "<<8<<" "<<6<<" "<<3<<" "<<7<<" "<<2<<" "<<4<<endl;
        cout<<1<<" "<<6<<" "<<8<<" "<<3<<" "<<7<<" "<<4<<" "<<2<<" "<<5<<endl;
        cout<<1<<" "<<7<<" "<<4<<" "<<6<<" "<<8<<" "<<2<<" "<<5<<" "<<3<<endl;
        cout<<92;
    }
    if(n==9){
        cout<<1<<" "<<3<<" "<<6<<" "<<8<<" "<<2<<" "<<4<<" "<<9<<" "<<7<<" "<<5<<endl;
        cout<<1<<" "<<3<<" "<<7<<" "<<2<<" "<<8<<" "<<5<<" "<<9<<" "<<4<<" "<<6<<endl;
        cout<<1<<" "<<3<<" "<<8<<" "<<6<<" "<<9<<" "<<2<<" "<<5<<" "<<7<<" "<<4<<endl;
        cout<<352;
    }
    if(n==10){
        cout<<1<<" "<<3<<" "<<6<<" "<<8<<" "<<10<<" "<<5<<" "<<9<<" "<<2<<" "<<4<<" "<<7<<endl;
        cout<<1<<" "<<3<<" "<<6<<" "<<9<<" "<<7<<" "<<10<<" "<<4<<" "<<2<<" "<<5<<" "<<8<<endl;
        cout<<1<<" "<<3<<" "<<6<<" "<<9<<" "<<7<<" "<<10<<" "<<4<<" "<<2<<" "<<8<<" "<<5<<endl;
        cout<<724;
    }
    if(n==11){
        cout<<1<<" "<<3<<" "<<5<<" "<<7<<" "<<9<<" "<<11<<" "<<2<<" "<<4<<" "<<6<<" "<<8<<" "<< 10<<endl;
        cout<<1<<" "<<3<<" "<<6<<" "<<9<<" "<<2<<" "<<8<<" "<<11<<" "<<4<<" "<<7<<" "<<5<<" "<<10<<endl;
        cout<<1<<" "<<3<<" "<<7<<" "<<9<<" "<<4<<" "<<2<<" "<<10<<" "<<6<<" "<<11<<" "<<5<<" "<<8<<endl;
        cout<<2680;
    }
    if(n==12){
        cout<<1<<" "<<3<<" "<<5<<" "<<8<<" "<<10<<" "<<12<<" "<<6<<" "<<11<<" "<<2<<" "<<7<<" "<<9<<" "<<4<<endl;
        cout<<1<<" "<<3<<" "<<5<<" "<<10<<" "<<8<<" "<<11<<" "<<2<<" "<<12<<" "<<6<<" "<<9<<" "<<7<<" "<<4<<endl;
        cout<<1<<" "<<3<<" "<<5<<" "<<10<<" "<<8<<" "<<11<<" "<<2<<" "<<12<<" "<<7<<" "<<9<<" "<<4<<" "<<6<<endl;
        cout<<14200;
    }
    if(n==13){
        cout<<1<<" "<<3<<" "<<5<<" "<<2<<" "<<9<<" "<<12<<" "<<10<<" "<<13<<" "<<4<<" "<<6<<" "<<8<<" "<<11<<" "<<7<<endl;
        cout<<1<<" "<<3<<" "<<5<<" "<<7<<" "<<9<<" "<<11<<" "<<13<<" "<<2<<" "<<4<<" "<<6<<" "<<8<<" "<<10<<" "<<12<<endl;
        cout<<1<<" "<<3<<" "<<5<<" "<<7<<" "<<12<<" "<<10<<" "<<13<<" "<<6<<" "<<4<<" "<<2<<" "<<8<<" "<<11<<" "<<9<<endl;
        cout<<73712; 
    }
    return 0;
}

by Rieman_sum @ 2024-08-16 11:02:59

这叫打表,太暴力了,虽然能过,但是不能体会到算法的本质,对你以后的学习很不好。这种方法在比赛时候迫不得已才用的


by __F__ @ 2024-08-16 11:07:14

@maizhe 又遇见你了


by maizhe @ 2024-08-16 11:08:54

@yuhan09 啥意思


by wizard(偷开O2 @ 2024-08-16 11:15:43

好厉害。


by chenkangqi1803 @ 2024-08-16 14:59:16

9(6翻了


by zhanghaoyu111 @ 2024-08-18 08:55:51

@maizhe

666


by zhangGAOsheng @ 2024-08-19 10:13:30

普通人玩不了也不想玩的招数


by tangyiqi @ 2024-08-20 14:56:20

@maizhe
又是你,你为什么这么喜欢打表??? 普通人玩不了也不想玩的玩法

#include <bits/stdc++.h>
using namespace std;
int a[15],n,ans;
bool check(int row,int col){
    for(int i = 1;i<row;i++){
        if(a[i] == col || abs(a[i]-col) == abs(row-i))return false;
    }
    return true;
}
void dfs(int k){
    if(k>n){ans++;if(ans>3)return;for(int i = 1;i<=n;i++)printf("%d ",a[i]);printf("\n");return;}
    for(int i = 1;i<=n;i++){
        if(check(k,i)){a[k] = i;dfs(k+1+1+1-2);a[k] = 0;}
    }
}
int main(){
    scanf("%d",&n);
    dfs(1);
    printf("%d ",ans);
}

求关,谢谢,按下关注后,请加入“https://www.luogu.com.cn/team/86926”团队


by hehe_666 @ 2024-09-06 10:06:35

@maizhe 你也太暴力了

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

int a[55], n, k, cnt; 
bool hang[55], lie[55], d1[55], d2[55];

void dfs(int step)
{
    if(step > n)
    {
        if(cnt <= 2)
        {
            for(int i = 1; i <= n; i++)
            {
                cout << a[i] << " ";
            }
            cout << endl;
        }
        cnt++;

    }
    for(int i = 1; i <= n; i++)
    {
        int x = step, y = i;
        if(hang[x] || lie[y] || d1[x - y + n] || d2[x + y]) continue;
        hang[x] = lie[y] = d1[x - y + n] = d2[x + y] = true;
        a[step] = i;
        dfs(step + 1);
        hang[x] = lie[y] = d1[x - y + n] = d2[x + y] = false;
    }
}

int main()
{
    cin >> n;
    k = 3;
    dfs(1);
    cout << cnt;
    return 0;
}

|