//~~~~~~~~~~~~~MJ®™~~~~~~~~~~~~~
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#define rep1(n) for(ll i=0; i<(ll)(n); ++i)
#define rep2(i,n) for(ll i=0; i<(ll)(n); ++i)
#define rep3(i,a,b) for(ll i=(ll)(a); i<(ll)(b); ++i)
#define rep4(i,a,b,c) for(ll i=(ll)(a); i<(ll)(b); i+=(c))
#define cut4(a,b,c,d,e,...) e
#define rep(...) cut4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define per1(n) for(ll i=((ll)n)-1; i>=0; --i)
#define per2(i,n) for(ll i=((ll)n)-1; i>=0; --i)
#define per3(i,a,b) for(ll i=((ll)a)-1; i>=(ll)(b); --i)
#define per4(i,a,b,c) for(ll i=((ll)a)-1; i>=(ll)(b); i-=(c))
#define per(...) cut4(__VA_ARGS__,per4,per3,per2,per1)(__VA_ARGS__)
#define ll long long
#define ln cout<<endl
#define int long long
#define Code ios_base::sync_with_stdio(0);
#define by cin.tie(NULL);
#define Hayan cout.tie(NULL);
#define append push_back
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define vi vector<int>
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define vb vector<bool>
#define vv vector<vi>
#define vp vector<pi>
#define ul map<int,vi>
#define ub map<int,bool>
#define ui map<int,int>
#define sum(a) accumulate(all(a),0)
#define add insert
#define endl '\n'
#define pi pair<int,int>
#define ff first
#define ss second
using namespace std;
int n,k,e;
vector<string> s,cp;
int merge(pi a, pi b)
{
int x,y;
x=a.ff,y=a.ss;
while (y+1<k and s[x][y+1]=='.')
{
y++;
}
if (y+1<k and (s[x][y+1]=='1' or (s[x][y+1]=='2')))
{
return 1;
}
cp=s;
cp[x][y]='-';
x=a.ff,y=a.ss;
cp[x][y]='-';
while (y-1>=0 and s[x][y-1]=='.')
{
y--;
}
if (y-1>=0 and (s[x][y-1]=='1' or (s[x][y-1]=='2')))
{
return 1;
}
cp[x][y]='-';
x=a.ff,y=a.ss;
while (x+1<e and s[x+1][y]=='.')
{
x++;
}
if (x+1<e and (s[x+1][y]=='1' or (s[x+1][y]=='2')))
{
return 1;
}
cp[x][y]='-';
x=a.ff,y=a.ss;
while (x-1>=0 and s[x-1][y]=='.')
{
x--;
}
if (x-1>=0 and (s[x-1][y]=='1' or (s[x-1][y]=='2')))
{
// cout<<a.ff<<" "<<a.ss<<" ";
// cout<<x-1<<" "<<" "<<y;
return 1;
// cout<<" hayan";
}
cp[x][y]='-';
for (int i=b.ff;i<e;i++)
{
if (cp[i][b.ss]=='x'){break;}
else if (cp[i][b.ss]=='-')
{
return 2;
}
}
for (int i=b.ff;i>=0;i--)
{
if (cp[i][b.ss]=='x'){break;}
else if (cp[i][b.ss]=='-')
{
return 2;
}
}
for (int i=b.ss;i<k;i++)
{
if (cp[b.ff][i]=='x'){break;}
else if (cp[b.ff][i]=='-')
{
return 2;
}
}
for (int i=b.ss;i>=0;i--)
{
if (cp[b.ff][i]=='x'){break;}
else if (cp[b.ff][i]=='-')
{
return 2;
}
}
return 3;
}
void solve()
{
cin>>n>>k>>e;
string t;
// vi a;
pi a,b;
rep(e){cin>>t;s.append(t);}
rep(e)
{
rep(j,0,k)
{
if (s[i][j]=='2')
{
b={i,j};
}
if (s[i][j]=='1')
{
// cout<<i<<" "<<j<<endl;
a={i,j};
}
}
}
int ans=min(merge(a,b),merge(b,a));
if (ans==3)
{
cout<<-1;
}
else {
cout<<ans;
}
// for (auto i: a){cout<<i<<" ";}
}
signed main(){
Code by Hayan
int ans=1;
//cout<<setprecision(1000);
// cin>>ans;
while(ans--){
// cout << "Case #" << ans << ": \n";
solve();ln;}}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |