输出为0,求调

P1746 离开中山路

CSPSDragon @ 2023-10-04 19:44:15

#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <math.h>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>//q.front 访问第一个
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef double dl;
typedef long long ll;
typedef long double ld;//小根堆:priority_queue<int,vector<int>,greater<int>> a;
typedef unsigned long long ull;//大根堆:priority_queue<int> a;
string s[1005];
bool book[1005][1005];
int n,fx[4][2]={0,1,1,0,0,-1,-1,0},x1,x2,y2,qy,ans;
struct node{
    int x;
    int y;
    int d;
};
queue<node> q;
void bfs()
{
    q.push({x1,qy,0});
    book[x1][qy]=1;
    while(!q.empty())
    {
        node t=q.front();
        q.pop() ;
        for(int i = 0;i <4;i++)
        {
            int xx=t.x +fx[i][0];
            int yy=t.y +fx[i][1]; 
            if (xx<0||yy<0||xx>=n||yy>=n) continue;
            //cerr << xx << " " <<yy <<" " << s[xx][yy] << " " <<book[xx][yy]<<" " << i <<endl; 
            //if(xx==2&&yy==2) cerr << t.d+1 << endl;
            if(xx>=0&&yy>=0&&xx<n&&yy<n&&s[xx][yy]=='0'&&book[xx][yy]==0)
            {
                if(xx==x2&&yy==y2)
                {
                    ans = t.d+1;
                    return ;
                }
                book[xx][yy]=1;
                q.push({xx,yy,t.d+1});
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >>n;
    for(int i = 1;i<=n;i++) cin >> s[i];
    cin >> x1 >> qy >> x2 >> y2; 
    x1--;
    qy--;
    x2--;
    y2--;
    bfs();
    cout << ans;
    return 0;
}

by CSPSDragon @ 2023-10-04 19:48:04

@Terry_MC 6


by Terry_MC @ 2023-10-04 19:51:47

#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <math.h>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>//q.front 访问第一个
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef double dl;
typedef long long ll;
typedef long double ld;//小根堆:priority_queue<int,vector<int>,greater<int>> a;
typedef unsigned long long ull;//大根堆:priority_queue<int> a;
string s[1005];
bool book[1005][1005];
int n,fx[4][2]={0,1,1,0,0,-1,-1,0},x1,x2,y2,qy,ans;
struct node{
    int x;
    int y;
    int d;
};
queue<node> q;
void bfs()
{
    q.push({x1,qy,0});
    book[x1][qy]=1;
    while(!q.empty())
    {
        node t=q.front();
        q.pop() ;
        if(t.x==x2&&t.y==y2)
        {
            ans = t.d;
            return ;
        }
        for(int i = 0;i <4;i++)
        {
            int xx=t.x +fx[i][0];
            int yy=t.y +fx[i][1]; 
            if (xx<0||yy<0||xx>=n||yy>=n) continue;
            //cerr << xx << " " <<yy <<" " << s[xx][yy] << " " <<book[xx][yy]<<" " << i <<endl; 
            //if(xx==2&&yy==2) cerr << t.d+1 << endl;
            if(xx>=0&&yy>=0&&xx<n&&yy<n&&s[xx][yy]=='0'&&book[xx][yy]==0)
            {
                book[xx][yy]=1;
                q.push({xx,yy,t.d+1});
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >>n;
    for(int i = 1;i<=n;i++) cin >> s[i];
    cin >> x1 >> qy >> x2 >> y2; 
    x1--;
    qy--;
    x2--;
    y2--;
    bfs();
    cout << ans;
    return 0;
}

这样可以60分,要是想要满分自己调!


by Terry_MC @ 2023-10-04 19:54:36

@zjs123bc_ 真的看看


by CSPSDragon @ 2023-10-04 20:02:08

我过了

此帖结

谢谢@Terry_MC!


|