做了一道题,对了,但是还是掉分了。
第二道题也做了,但是没有交上,不知道对错。
后来交上以后发现少判断了一个条件,改过之后就对了。
第一道题爆搜的,有点麻烦了,其实几行代码就行。
250贴代码:
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#define LL long long
using namespace std; class BishopMove
{
public:
struct node
{
int x, y, step;
} next, pos;
int howManyMoves(int r1, int c1, int r2, int c2)
{
int vis[][], i, a, b;
memset(vis, , sizeof(vis));
if(r1==r2 && c1==c2)
return ; queue<node>q;
vis[r1][c1] = ;
next.x = r1;
next.y = c1;
next.step = ;
q.push(next);
while(!q.empty())
{
pos = q.front();
if(pos.x == r2 && pos.y == c2)
return pos.step; q.pop();
for(i = ; i <= ; i ++)
{
a = pos.x+i;
b = pos.y+i;
if(a>=&&a<= && b>= && b<= && vis[a][b]==)
{
vis[a][b] = ;
next.x = a;
next.y = b;
next.step = pos.step+;
q.push(next);
}
a = pos.x+i;
b = pos.y-i;
if(a>=&&a<= && b>= && b<= && vis[a][b]==)
{
vis[a][b] = ;
next.x = a;
next.y = b;
next.step = pos.step+;
q.push(next);
}
a = pos.x-i;
b = pos.y+i;
if(a>=&&a<= && b>= && b<= && vis[a][b]==)
{
vis[a][b] = ;
next.x = a;
next.y = b;
next.step = pos.step+;
q.push(next);
}
a = pos.x-i;
b = pos.y-i;
if(a>=&&a<= && b>= && b<= && vis[a][b]==)
{
vis[a][b] = ;
next.x = a;
next.y = b;
next.step = pos.step+;
q.push(next);
}
}
}
return -;
}
}; int main()
{
int r1, c1, r2, c2;
while()
{
cin>>r1>>c1>>r2>>c2;
BishopMove y;
cout<<y.howManyMoves(r1, c1, r2, c2)<<endl;
}
return ;
}
题意:有三种括号 和 x,x能变成任意的括号,求能否通过变化x使得给的字符串符合括号匹配
500贴代码:
AC代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <vector>
#define LL long long
using namespace std; class BracketExpressions
{
public:
int _max(int c, int d)
{
return c > d?c:d;
}
int match(char a, char b)
{
if(a=='(' && b==')')
return ;
if(a=='{' && b=='}')
return ;
if(a=='[' && b==']')
return ;
if(a=='X' &&(b==']'||b=='}'||b==')'))
return ;
if(b=='X' && (a=='['||a=='{'||a=='('))
return ;
if(a=='X' && b=='X')
return ;
return ;
}
string ifPossible(string expression)
{
int i, j, k, g, len;
int d[][];
string s = expression;
len = s.size();
memset(d, , sizeof(d));
for(i = ; i < len-; i++)
if(match(s[i], s[i+]))
d[i][i+] = ;
for(k = ; k < len; k++)
{
for(i = ; i < len-k; i++)
{
j = i+k;
if(match(s[i], s[j])) d[i][j] = d[i+][j-] + ;
for(g = ; g < k; g++)
d[i][j] = _max(d[i][i+g]+d[i+g+][j], d[i][j]);
}
}
if(*d[][len-]!=len)
return "impossible";
else
return "possible";
}
};