广搜
1.(第二周A题)
题目描述
原题来自:POJ 1915
编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数。骑士一步可以移动到的位置由下图给出。
输入格式
第一行给出骑士的数量 nn。
在接下来的 3n3n 行中,每 33 行描述了一个骑士。其中,
- 第一行一个整数 LL 表示棋盘的大小,整个棋盘大小为 L\times LL×L;
- 第二行和第三行分别包含一对整数 (x,y)(x,y),表示骑士的起始点和终点。假设对于每一个骑士,起始点和终点均合理。
输出格式
对每一个骑士,输出一行一个整数表示需要移动的最小步数。如果起始点和终点相同,则输出 00。
样例
Input | Output |
---|---|
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1 | 5 28 0 |
数据范围与提示
对于 100\%100% 的数据,有 4\le L\le 3004≤L≤300,保证 0\le x,y\le L-10≤x,y≤L−1。
思路:广度优先搜索
代码:
#include<bits/stdc++.h>
using namespace std;
int a[310][310],n;
int next1[8][2]={1,2,1,-2,-1,2,-1,-2,-2,-1,-2,1,2,1,2,-1};
typedef struct
{
int x;
int y;
int s;
}q;
queue<q>p;
void dfs(int sx,int sy,int ex,int ey)
{
while (!p.empty())
{
q t=p.front();
p.pop();
int x,y,s;
x=t.x;
y=t.y;
s=t.s;
if (x==ex &&y==ey)
{
cout<<s<<endl;
break;
}
for (int i=0;i<8;i++)
{
int nx=x+next1[i][0];
int ny=y+next1[i][1];
if (nx<0||nx>=n||ny<0||ny>=n||a[nx][ny]==1)
continue;
else
{
q t;
a[nx][ny]=1;
t.x=nx;
t.y=ny;
t.s=s+1;
p.push(t);
}
}
}
return ;
}
int main()
{
int t;
int sx,sy,ex,ey;
scanf("%d",&t);
while(t--)
{
cin>>n;
cin>>sx>>sy>>ex>>ey;
memset(a,0,sizeof(a));
a[sx][sy]=1;
q m;
m.x=sx;
m.y=sy;
m.s=0;
p.push(m);
dfs(sx,sy,ex,ey);
while (!p.empty()) p.pop();
}
return 0;
}
2.(第二周J题)
A string is called square if it is some string written twice in a row. For example, the strings "aa", "abcabc", "abab" and "baabaa" are square. But the strings "aaa", "abaaab" and "abcdabc" are not square.
For a given string ss determine if it is square.
Input
The first line of input data contains an integer tt (1 \le t \le 1001≤t≤100) —the number of test cases.
This is followed by tt lines, each containing a description of one test case. The given strings consist only of lowercase Latin letters and have lengths between 11 and 100100 inclusive.
Output
For each test case, output on a separate line:
- YES if the string in the corresponding test case is square,
- NO otherwise.
You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
Example
Input
10 a aa aaa aaaa abab abcabc abacaba xxyy xyyx xyxy
Output
NO YES NO YES YES YES NO NO NO YES
法一:
代码:
#include<bits/stdc++.h>
using namespace std;
int a[310][310],n;
int next1[8][2]={1,2,1,-2,-1,2,-1,-2,-2,-1,-2,1,2,1,2,-1};
typedef struct
{
int x;
int y;
int s;
}q;
queue<q>p;
void dfs(int sx,int sy,int ex,int ey)
{
while (!p.empty())
{
q t=p.front();
p.pop();
int x,y,s;
x=t.x;
y=t.y;
s=t.s;
if (x==ex &&y==ey)
{
cout<<s<<endl;
break;
}
for (int i=0;i<8;i++)
{
int nx=x+next1[i][0];
int ny=y+next1[i][1];
if (nx<0||nx>=n||ny<0||ny>=n||a[nx][ny]==1)
continue;
else
{
q t;
a[nx][ny]=1;
t.x=nx;
t.y=ny;
t.s=s+1;
p.push(t);
}
}
}
return ;
}
int main()
{
int t;
int sx,sy,ex,ey;
scanf("%d",&t);
while(t--)
{
cin>>n;
cin>>sx>>sy>>ex>>ey;
memset(a,0,sizeof(a));
a[sx][sy]=1;
q m;
m.x=sx;
m.y=sy;
m.s=0;
p.push(m);
dfs(sx,sy,ex,ey);
while (!p.empty()) p.pop();
}
return 0;
}
法二:
思路:方形长度为偶数,先算出字符串长度 从中间分开,对比前后是否相同。
代码:
#include<stdio.h>
#include<string.h>
int main()
{
int m,n,i,j,t,k,x,flag=0;
char s[150],a[150],b[150];
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
t=strlen(s);
flag=0;
if(t%2==0)
{
k=t/2;
x=k;
for(j=0;j<k;j++)
a[j]=s[j];
for(j=k;j<t;j++)
b[j]=s[j];
for(j=0;j<k;j++)
{
if(a[j]!=b[x])
{
flag=1;
break;
}
x++;
}
if(flag==0)
printf("YES\n");
else
printf("NO\n");
}
else
printf("NO\n");
}
return 0;
}