usaco The Clocks

本文介绍了一个基于九宫格时钟谜题的求解算法,通过使用9层循环来穷举所有可能的操作组合,寻找能够将初始状态的九宫格时钟调整到目标状态(所有时钟指向12点)的方法。该算法涉及数组操作、文件输入输出等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没想到好方法,9个循环……


#include <iostream>
#include <fstream>
#include <string.h>
int ans[4][4]={0};
int a[10];
int tr[4][4]={0};
bool check();
void move(int,int);
void movehelp(int a[][4],int,int);
using namespace std;
int main()
{
 ifstream cin("clocks.in");
 ofstream cout("clocks.out");
 int i,j;
 for(i=1;i<=3;i++)
  for(j=1;j<=3;j++)
   cin>>ans[i][j];
 for(a[1]=0;a[1]<4;a[1]++)
  for(a[2]=0;a[2]<4;a[2]++)
   for(a[3]=0;a[3]<4;a[3]++)
    for(a[4]=0;a[4]<4;a[4]++)
     for(a[5]=0;a[5]<4;a[5]++)
      for(a[6]=0;a[6]<4;a[6]++)
       for(a[7]=0;a[7]<4;a[7]++)
        for(a[8]=0;a[8]<4;a[8]++)
         for(a[9]=0;a[9]<4;a[9]++)
          if(check())
          {
           int i=1,j;
           for(i;i<=9;i++)
                 {
               if(a[i]!=0)
                  {
                for(j=0;j<a[i];j++)
                {
                 if(j!=a[i]-1)
                 cout<<i<<" ";
                 else
                 cout<<i;
                }
                break;
               }
            }
           i++;
           for(i;i<=9;i++)
            if(a[i]>0)
             {
             for(j=0;j<a[i];j++)
              cout<<" "<<i;
             }
           cout<<endl;
           return 0;
          }
}
bool check()
{
 int i,j;
 for(i=1;i<=3;i++)
  for(j=1;j<=3;j++)
   tr[i][j]=ans[i][j];
 for(i=1;i<=9;i++)
  move(i,a[i]);
 int k;
 for(i=1;i<=3;i++)
  for(j=1;j<=3;j++)
           if(tr[i][j]!=12)
      return 0;
 return 1;
}
void move(int num,int much)
{
 int i,j;
    if(num==1)
 {
  for(i=0;i<much;i++)
   movehelp(tr,1,1);
  for(i=0;i<much;i++)
   movehelp(tr,1,2);
  for(i=0;i<much;i++)
   movehelp(tr,2,1);
  for(i=0;i<much;i++)
   movehelp(tr,2,2);
 }
 else if(num==2)
 {
  for(i=0;i<much;i++)
   movehelp(tr,1,1);
  for(i=0;i<much;i++)
   movehelp(tr,1,2);
  for(i=0;i<much;i++)
   movehelp(tr,1,3);
 }
 else if(num==3)
 {
  for(i=0;i<much;i++)
   movehelp(tr,1,2);
  for(i=0;i<much;i++)
   movehelp(tr,1,3);
  for(i=0;i<much;i++)
   movehelp(tr,2,2);
  for(i=0;i<much;i++)
   movehelp(tr,2,3);
 }
 else if(num==4)
 {
  for(i=0;i<much;i++)
   movehelp(tr,1,1);
  for(i=0;i<much;i++)
   movehelp(tr,2,1);
  for(i=0;i<much;i++)
   movehelp(tr,3,1);
 }
 else if(num==5)
 {
  for(i=0;i<much;i++)
   movehelp(tr,1,2);
  for(i=0;i<much;i++)
   movehelp(tr,2,1);
  for(i=0;i<much;i++)
   movehelp(tr,2,2);
  for(i=0;i<much;i++)
   movehelp(tr,2,3);
  for(i=0;i<much;i++)
   movehelp(tr,3,2);
 }
 else if(num==6)
 {
  for(i=0;i<much;i++)
   movehelp(tr,1,3);
  for(i=0;i<much;i++)
   movehelp(tr,2,3);
  for(i=0;i<much;i++)
   movehelp(tr,3,3);
 }
 else if(num==7)
 {
  for(i=0;i<much;i++)
   movehelp(tr,2,1);
  for(i=0;i<much;i++)
   movehelp(tr,2,2);
  for(i=0;i<much;i++)
   movehelp(tr,3,1);
  for(i=0;i<much;i++)
   movehelp(tr,3,2);
 }
 else if(num==8)
 {
  for(i=0;i<much;i++)
   movehelp(tr,3,1);
  for(i=0;i<much;i++)
   movehelp(tr,3,2);
  for(i=0;i<much;i++)
   movehelp(tr,3,3);
 }
 else if(num==9)
 {
  for(i=0;i<much;i++)
   movehelp(tr,2,2);
  for(i=0;i<much;i++)
   movehelp(tr,2,3);
  for(i=0;i<much;i++)
   movehelp(tr,3,2);
  for(i=0;i<much;i++)
   movehelp(tr,3,3);
 }
}
void movehelp(int tr[][4],int i,int j)
{
 if(tr[i][j]!=12)
  tr[i][j]+=3;
 else
     tr[i][j]=3;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值