卡常求助————

#include <bits/stdc++.h>
#define int long long
#define R register
#define F(i,a,b) for(int i = (a);i<=(b);i++)
using namespace std;
inline int read(){R int x=0,t=1;R char ch=getchar();while(ch<‘0’||ch>‘9’){if(ch==‘-’) t=-1;ch=getchar();}while(ch>=‘0’&&ch<=‘9’){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return xt;}
int n,m,t,a[55][55],ans,vis[55][55][55][55];
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
struct Node
{
int x,y,cnt,lx,ly;
};
int ex,ey,sx,sy,tx,ty;
queueq;//空白格子的位置
queuend;//需要的格子的位置(与q同步)
void put1(int a,int b,int c,int e,int f)
{
Node X;
X.x=a,X.y=b,X.cnt=c,X.lx=e,X.ly=f;
q.push(X);
}
void put2(int a,int b,int c)
{
Node X;
X.x=a,X.y=b,X.cnt=c;
nd.push(X);
}
void bfs()//懒得打参数了’w’
{
if(sx==tx && sy==ty)
{
cout << 0<<“\n”;
return;
}
for(R int i = 1;i<=n;i++)
{
for(R int i1 = 1;i1<=m;i1++)
{
for(R int i2 = 1;i2<=n;i2++){
for(R int i3 = 1;i3<=m;i3++){
vis[i][i1][i2][i3]^=vis[i][i1][i2][i3];
}
}
}
}
q.push({ex,ey,0});
nd.push({sx,sy,0});
vis[sx][sy][ex][ey]=1;
while(q.size()){
Node u1=q.front(),u2=nd.front();
// cout << “---------------------\n”;
// cout << u1.x << " " << u1.y << ‘\n’;
// cout << u2.x << " " << u2.y << ‘\n’;
// cout << u1.cnt << ‘\n’;
for(R int i = 0;i<4;i++){//4个方向
int vx=u1.x+dx[i],vy=u1.y+dy[i];
if(vx>=1 && vx<=n && vy <= m && vy>=1 && a[vx][vy]==1)
{
if(vx==u2.x && vy==u2.y)
{
if(vx == tx && vy==ty)
{
//cout << i << “#\n”;
cout << u1.cnt << “\n”;
return;
}
if(!vis[u1.x][u1.y][vx][vy]){
vis[u1.x][u1.y][vx][vy]=1;
put1(vx,vy,u1.cnt+1,u1.x,u1.y);
put2(u1.x,u1.y,u1.cnt+1);
}
}
else
{
if(!vis[u2.x][u2.y][vx][vy])
{
vis[u2.x][u2.y][vx][vy]=1;
put1(vx,vy,u1.cnt+1,u1.x,u1.y);
put2(u2.x,u2.y,u1.cnt+1);
}
}
}
}
q.pop(),nd.pop();
}
cout << -1;
puts(“”);
}
inline void solve()
{
n=read(),m=read(),t=read();
F(i,1,n){
F(j,1,m){
a[i][j]=read();
}
}
F(i,1,t){
ex=read(),ey=read(),sx=read(),sy=read(),tx=read(),ty=read();
bfs();
while(q.size())
{
q.pop();
}
while(nd.size())
{
nd.pop();
}
}
}
signed main()
{
solve();
return 0;
}
/

while(!qt.empty()&&flag){
node now=qt.front();
for(int i=0;i<4;i++){
int nx=now.hx+dx[i],ny=now.hy+dy[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&b[nx][ny]){
if(nx==now.x&&ny==now.y){
if(now.hx==tx&&now.hy==ty){
printf(“%d\n”,now.s+1);
flag=0;
break;
}
if(!vis[now.hx][now.hy][nx][ny]){
vis[now.hx][now.hy][nx][ny]=1;
qt.push({now.hx,now.hy,nx,ny,now.s+1});
}
}else{
if(!vis[now.x][now.y][nx][ny]){
vis[now.x][now.y][nx][ny]=1;
qt.push({now.x,now.y,nx,ny,now.s+1});
}
}
}
}
qt.pop();
}

*/
卡常失败只有60pts

1 个赞

题目有吗

1 个赞

我看不见

1 个赞

#include <bits/stdc++.h>
#define int long long
#define R register
#define F(i,a,b) for(int i = (a);i<=(b);i++)
using namespace std;
inline int read(){R int x=0,t=1;R char ch=getchar();while(ch<‘0’||ch>‘9’){if(ch==‘-’) t=-1;ch=getchar();}while(ch>=‘0’&&ch<=‘9’){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return xt;}
int n,m,t,a[55][55],ans,vis[55][55][55][55];
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
struct Node
{
int x,y,cnt,lx,ly;
};
int ex,ey,sx,sy,tx,ty;
queueq;//空白格子的位置
queuend;//需要的格子的位置(与q同步)
void put1(int a,int b,int c,int e,int f)
{
Node X;
X.x=a,X.y=b,X.cnt=c,X.lx=e,X.ly=f;
q.push(X);
}
void put2(int a,int b,int c)
{
Node X;
X.x=a,X.y=b,X.cnt=c;
nd.push(X);
}
void bfs()//懒得打参数了’w’
{
if(sx==tx && sy==ty)
{
cout << 0<<“\n”;
return;
}
for(R int i = 1;i<=n;i++)
{
for(R int i1 = 1;i1<=m;i1++)
{
for(R int i2 = 1;i2<=n;i2++){
for(R int i3 = 1;i3<=m;i3++){
vis[i][i1][i2][i3]^=vis[i][i1][i2][i3];
}
}
}
}
q.push({ex,ey,0});
nd.push({sx,sy,0});
vis[sx][sy][ex][ey]=1;
while(q.size()){
Node u1=q.front(),u2=nd.front();
// cout << “---------------------\n”;
// cout << u1.x << " " << u1.y << ‘\n’;
// cout << u2.x << " " << u2.y << ‘\n’;
// cout << u1.cnt << ‘\n’;
for(R int i = 0;i<4;i++){//4个方向
int vx=u1.x+dx[i],vy=u1.y+dy[i];
if(vx>=1 && vx<=n && vy <= m && vy>=1 && a[vx][vy]==1)
{
if(vx==u2.x && vy==u2.y)
{
if(vx == tx && vy==ty)
{
//cout << i << “#\n”;
cout << u1.cnt << “\n”;
return;
}
if(!vis[u1.x][u1.y][vx][vy]){
vis[u1.x][u1.y][vx][vy]=1;
put1(vx,vy,u1.cnt+1,u1.x,u1.y);
put2(u1.x,u1.y,u1.cnt+1);
}
}
else
{
if(!vis[u2.x][u2.y][vx][vy])
{
vis[u2.x][u2.y][vx][vy]=1;
put1(vx,vy,u1.cnt+1,u1.x,u1.y);
put2(u2.x,u2.y,u1.cnt+1);
}
}
}
}
q.pop(),nd.pop();
}
cout << -1;
puts(“”);
}
inline void solve()
{
n=read(),m=read(),t=read();
F(i,1,n){
F(j,1,m){
a[i][j]=read();
}
}
F(i,1,t){
ex=read(),ey=read(),sx=read(),sy=read(),tx=read(),ty=read();
bfs();
while(q.size())
{
q.pop();
}
while(nd.size())
{
nd.pop();
}
}
}
signed main()
{
solve();
return 0;
}
/

while(!qt.empty()&&flag){
node now=qt.front();
for(int i=0;i<4;i++){
int nx=now.hx+dx[i],ny=now.hy+dy[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&b[nx][ny]){
if(nx==now.x&&ny==now.y){
if(now.hx==tx&&now.hy==ty){
printf(“%d\n”,now.s+1);
flag=0;
break;
}
if(!vis[now.hx][now.hy][nx][ny]){
vis[now.hx][now.hy][nx][ny]=1;
qt.push({now.hx,now.hy,nx,ny,now.s+1});
}
}else{
if(!vis[now.x][now.y][nx][ny]){
vis[now.x][now.y][nx][ny]=1;
qt.push({now.x,now.y,nx,ny,now.s+1});
}
}
}
}
qt.pop();
}

*/

P1979 [NOIP2013 提高组] 华容道 洛谷

1 个赞

据说直接宽搜能过,但只有60pts

1 个赞

格式最好改一下,这样好看。