八皇后 Checker Challenge
## 题目描述
一个如下的 $6 \times 6$ 的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。
![](https://cdn.luogu.com.cn/upload/pic/60.png)
上面的布局可以用序列 $2\ 4\ 6\ 1\ 3\ 5$ 来描述,第 $i$ 个数字表示在第 $i$ 行的相应位置有一个棋子,如下:
行号 $1\ 2\ 3\ 4\ 5\ 6$
列号 $2\ 4\ 6\ 1\ 3\ 5$
这只是棋子放置的一个解。请编一个程序找出所有棋子放置的解。
并把它们以上面的序列方法输出,解按字典顺序排列。
请输出前 $3$ 个解。最后一行是解的总个数。## 输入格式
一行一个正整数 $n$,表示棋盘是 $n \times n$ 大小的。
## 输出格式
前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。
## 样例 #1
### 样例输入 #1
```
6
```### 样例输出 #1
```
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
```## 提示
【数据范围】
对于 $100\%$ 的数据,$6 \le n \le 13$。题目翻译来自NOCOW。
USACO Training Section 1.5
思路:我们只需要输出棋子的列坐标;因此我们可以不开辟二维数组 ,开辟一维数组模拟二维数组,开辟对角线的数组来控制棋子的位置
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//不开辟二维数组,用2个一维数组模拟二维数组
int x[100],y[100],z[100],j[100],n,cnt=0;//分别控制行和列和对角线(j,z分别表示减的那根和增的那根)
void print()
{
if(cnt<=3)//只输出3次
{
for(int i=1;i<=n;i++)
{
printf("%d ",x[i]);
}
puts("");
}
}
void dfs(int h)//s为行
{
if(h==n+1)//如果h=n+1时,n行已经全部放置完毕,进行输出;
{
cnt++;//解的个数+1
print();
return ;
}
for(int i=1;i<=n;i++)//对列进行搜索
{
if(y[i]==0&&z[h+i]==0&&j[h+n-i]==0)//该点对角线,列行都未被占领
{
x[h]=i;//皇后的列信息存入数组x
y[i]=1;//第i列被占领
//对角线被占领
z[h+i]=1;//递增的那根对角线(我们通过控制其坐标对应的值控制整条对角线,我们可以发现递增的过
//程中,行坐标数值减小,列坐标数值增大,且增减数值的大小不变,因此同一条对角线上行列坐标的和始终不变)
j[h+n-i]=1; //递减的这根对角线,我们发现递减的过程中列坐标的值在增加,行坐标的值也在增加
//我们将纵坐标i改为n-i,此时i增加,n-i是减小的,所以同递增的对角线其和始终保持不变,从而控制整条对角线
dfs(h+1);//前往下一行
//回溯
y[i]=0;//列打开
//对角线设置为0
z[h+i]=0;
j[h+n-i]=0;
}
}
}
int main()
{
scanf("%d",&n);//n行n列
dfs(1);//从第一行开始搜索
printf("%d",cnt);
return 0;
}
验证栈序列
## 题目描述
给出两个序列 pushed 和 poped 两个序列,其取值从 1 到 $n(n\le100000)$。已知入栈序列是 pushed,如果出栈序列有可能是 poped,则输出 `Yes`,否则输出 `No`。为了防止骗分,每个测试点有多组数据。
## 输入格式
第一行一个整数 $q$,询问次数。
接下来 $q$ 个询问,对于每个询问:
第一行一个整数 $n$ 表示序列长度;
第二行 $n$ 个整数表示入栈序列;
第三行 $n$ 个整数表示出栈序列;
## 输出格式
对于每个询问输出答案。
## 样例 #1
### 样例输入 #1
```
2
5
1 2 3 4 5
5 4 3 2 1
4
1 2 3 4
2 4 1 3
```### 样例输出 #1
```
Yes
No
```
思路:第一眼看到这个题以为是逆序和正序,我在想不就是水题吗?随后WA好几次啊!!原来这个题题意是入栈的同时可以出栈;
例如:4 3 2 1
的合法栈序列不仅仅只有1 2 3 4
还有 2 1 3 4等等
1入栈,2 入栈,随后 2出栈 1出栈再是 3入栈 3 出栈 4 入栈 4 出栈也是符合的
我们可以开辟一个新的数组去存放入栈的数列,用一个变量去指向栈顶(有点像队列不过只有队首)模拟栈的出入。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int a[100001],b[100001],c[100001];//a数组用于入栈序列的输入,//b数组用于出栈序列
//c数组用于模拟栈出入的容器
int main()
{
int q,n,k=0,tail=0;
scanf("%d",&q);
for(int i=0;i<q;i++)//q次询问
{
scanf("%d",&n);//输入
for(int j=0;j<n;j++)
{
scanf("%d",a+j);
}
for(int j=0;j<n;j++)
{
scanf("%d",b+j);
}
//work
k=0,tail=0;//初始化一定不要忘记了
for(int j=0;j<n;j++)
{
c[tail++]=a[j];//入栈(存入c数组)
while(c[tail-1]==b[k])//tail有自增,所以这里是tail-1
{
//和出栈序列相等的时候
k++;//指向出栈序列的下一位
tail--;//栈顶下降
if(tail==0)break;
}
}
if(tail==0)printf("Yes\n");//栈为空
else printf("No\n");//栈不为空
}
}
/*
4
4
1 2 3 4
4 3 2 1
4
1 2 3 4
2 1 4 3
*/
Corn Maze S
# [USACO11OPEN]Corn Maze S
## 题面翻译
奶牛们去一个 $N\times M$ 玉米迷宫,$2 \leq N \leq 300,2 \leq M \leq300$。
迷宫里有一些传送装置,可以将奶牛从一点到另一点进行瞬间转移。这些装置可以双向使用。
如果一头奶牛处在这个装置的起点或者终点,这头奶牛就**必须**使用这个装置。
玉米迷宫除了唯一的一个出口都被玉米包围。
迷宫中的每个元素都由以下项目中的一项组成:
1. 玉米,`#` 表示,这些格子是不可以通过的。
1. 草地,`.` 表示,可以简单的通过。
1. 传送装置,每一对大写字母 $\tt{A}$ 到 $\tt{Z}$ 表示。
1. 出口,`=` 表示。
1. 起点, `@` 表示奶牛能在一格草地上可能存在的四个相邻的格子移动,花费 $1$ 个单位时间。从装置的一个结点到另一个结点不花时间。
## 题目描述
This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in the maze to another. The slides work in both directions: a cow can slide from the slide's start to the end instantly, or from the end to the start. If a cow steps on a space that hosts either end of a slide, she must use the slide.
The outside of the corn maze is entirely corn except for a single exit.
The maze can be represented by an N x M (2 <= N <= 300; 2 <= M <= 300) grid. Each grid element contains one of these items:
\* Corn (corn grid elements are impassable)
\* Grass (easy to pass through!)
\* A slide endpoint (which will transport a cow to the other endpoint)
\* The exit
A cow can only move from one space to the next if they are adjacent and neither contains corn. Each grassy space has four potential neighbors to which a cow can travel. It takes 1 unit of time to move from a grassy space to an adjacent space; it takes 0 units of time to move from one slide endpoint to the other.
Corn-filled spaces are denoted with an octothorpe (#). Grassy spaces are denoted with a period (.). Pairs of slide endpoints are denoted with the same uppercase letter (A-Z), and no two different slides have endpoints denoted with the same letter. The exit is denoted with the equals sign (=).
Bessie got lost. She knows where she is on the grid, and marked her current grassy space with the 'at' symbol (@). What is the minimum time she needs to move to the exit space?
## 输入格式
第一行:两个用空格隔开的整数 $N$ 和 $M$。
第 $2\sim N+1$ 行:第 $i+1$ 行描述了迷宫中的第 $i$ 行的情况(共有$M$个字符,每个字符中间没有空格)。
## 输出格式
一个整数,表示起点到出口所需的最短时间。
## 样例 #1
### 样例输入 #1
```
5 6
###=##
#.W.##
#.####
#.@W##
######
```### 样例输出 #1
```
3
```## 提示
例如以下矩阵,$N=5,M=6$。
```plain
###=##
#.W.##
#.####
#.@W##
######
```唯一的一个装置的结点用大写字母 $\tt{W}$ 表示。
最优方案为:先向右走到装置的结点,花费一个单位时间,再到装置的另一个结点上,花费 $0$ 个单位时间,然后再向右走一个,再向上走一个,到达出口处,总共花费了 $3$ 个单位时间。
万恶的玉米地,改bug改了一个整整的下午加晚上2小时
思路:找出起点和终点,和传送的点的坐标,可以少很多个循环,数据比较大我们采用bfs进行搜索,话不多说我们一起来看万恶的玉米地
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char ch[305][305];
int book[305][305];
int flag[27];//用于传送点坐标的存取的判断(判断是第1个还是第二个传送点)
struct node
{
int x,y;//坐标
int s;//步数
}que[300*300+5];
struct cs
{
int x1,y1;//传送点入口坐标
int x2,y2;//传送点出口坐标
}c[26];
int main()
{
int N,M,sx,sy,tx,ty,zx,zy,flag1=0;
scanf("%d%d",&N,&M);
getchar();//这里不要忘记了啊!!!(每日血的教训)
//平平无奇的输入(却又干了很多事情)
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
scanf("%c",&ch[i][j]);
//输出的时候将起点坐标找到
if(ch[i][j]=='@')//起点坐标
{
sx=i;
sy=j;
}
if(ch[i][j]=='=')//终点坐标
{
zx=i;
zy=j;
}
if(ch[i][j]>='A'&&ch[i][j]<='Z')//传送点的坐标存好
{
if(flag[ch[i][j]-'A']==0)//一个传送点//存入1号
{//这里利用了桶排的思想存入传送点的坐标
c[ch[i][j]-'A'].x1=i;
c[ch[i][j]-'A'].y1=j;
flag[ch[i][j]-'A']=1;//记得要标记啊!!!(别问,问就是忘记了)
}
else //对应的传送点//存入2号
{
c[ch[i][j]-'A'].x2=i;
c[ch[i][j]-'A'].y2=j;
}
}
}
getchar();//一定不要忘记了啊,我就又忘记了。
}
int head=1,tail=1;
int next[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
que[tail].x=sx;//起点入队
que[tail].y=sy;
que[tail].s=0;//初始步数
book[sx][sy]=1;
tail++;//扩充队尾
while(head<tail)
{
for(int k=0;k<4;k++)//四个方向
{
tx=que[head].x+next[k][0];
ty=que[head].y+next[k][1];
if(tx<1||ty<1||tx>N||ty>M)//判断是否越界
{
continue;//如果越界了则尝试下一种做法
}
if(ch[tx][ty]!='#'&&book[tx][ty]==0)//不是围墙没有来过
{
book[tx][ty]=1;
if(ch[tx][ty]>='A'&&ch[tx][ty]<='Z')//遇到传送点
{
if((tx==c[ch[tx][ty]-'A'].x1)&&(ty==c[ch[tx][ty]-'A'].y1))//判断是否处于1号传送点
{//不处于1号传送点的时候
int t1=tx,t2=ty;
//这里一定一定要注意了,要设置中间变量存下标,因为随后tx的值会改变
//找了好久好久好久好久
tx=c[ch[t1][t2]-'A'].x2;
ty=c[ch[t1][t2]-'A'].y2;
que[tail].x=tx;//入队
que[tail].y=ty;
que[tail].s=que[head].s+1;
tail++;//扩充队尾
}else//处于2号传送点的时候
{
//这里也一样,谁懂改了上面没改下面,又WA了,而且又找了10多分钟。
int t1=tx,t2=ty;
tx=c[ch[t1][t2]-'A'].x1;
ty=c[ch[t1][t2]-'A'].y1;
que[tail].x=tx;//入队
que[tail].y=ty;
que[tail].s=que[head].s+1;
tail++; //扩充队尾
}
}
else//不是传送点(这里事唯一没有修改过的区段了,蒻蒻的我)
{
book[tx][ty]=1;
que[tail].x=tx;
que[tail].y=ty;
que[tail].s=que[head].s+1;
tail++;
}
}
if(tx==zx&&ty==zy)//到达终点
{
flag1=1;
break;
}
}
head++;//出队
if(flag1==1)break;
}
printf("%d",que[tail-1].s);//上面tail完成了一次自增,所以这里事tail-1
}
/*\
5 6
######
#..W##
#=####
#.@W##
######
*/