-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b6426a
commit d374091
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <list> | ||
#include <vector> | ||
#include <queue> | ||
using namespace std; | ||
|
||
int X,Y; | ||
|
||
void citeste(vector<list<int>> &graf) | ||
{ | ||
int n,m,x,y; | ||
ifstream f("graf.in"); | ||
f>>n>>m>>X>>Y; | ||
graf.resize(n+1); | ||
for(int i=0;i<m;i++){ | ||
f>>x>>y; | ||
graf[x].push_back(y); | ||
graf[y].push_back(x); | ||
} | ||
|
||
} | ||
|
||
void calcDistante(int start,vector<list<int>>& graf,vector<int> &dist){ | ||
dist.resize(graf.size()); | ||
queue<int> q; | ||
q.push(start); | ||
dist[start]=1; | ||
while(!q.empty()){ | ||
int acc = q.front(); | ||
q.pop(); | ||
for(int i : graf[acc]){ | ||
if(!dist[i]){ | ||
dist[i]=dist[acc]+1; | ||
q.push(i); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
int main(){ | ||
vector<int>frecv; | ||
vector<int> solutie; | ||
vector<list<int>> graf; | ||
citeste(graf); | ||
frecv.resize(graf.size(),0); | ||
vector<int> distX,distY; | ||
|
||
calcDistante(X,graf,distX); | ||
calcDistante(Y,graf,distY); | ||
|
||
for(int i=1;i<graf.size();i++){ | ||
if(distX[i]+distY[i]==distX[Y]+1) | ||
frecv[distX[i]]++; | ||
} | ||
for(int i =1;i<graf.size();i++){ | ||
if(distX[i]+distY[i]==distX[Y]+1 && frecv[distX[i]]==1)solutie.push_back(i); | ||
} | ||
ofstream g("graf.out"); | ||
g<<solutie.size()<<endl; | ||
for(int i:solutie) | ||
g<<i<<" "; | ||
g.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <queue> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <cstring> | ||
#include <iomanip> | ||
using namespace std; | ||
#define inf 1e5+5; | ||
|
||
ifstream f("rj.in"); | ||
ofstream g("rj.out"); | ||
int n,m,a[101][101],romeo[101][101],julieta[101][101]; | ||
int di[8] = {0, 0, -1, -1, -1, 1, 1, 1}; | ||
int dj[8] = {1, -1, -1, 1, 0, -1, 0, 1}; | ||
|
||
void fill(pair<int,int> pos , int matrice[101][101]){ | ||
int nextX,nextY; | ||
queue<pair<int,int>> q; | ||
matrice[pos.first][pos.second] = 1; | ||
q.push(pos); | ||
while(!q.empty()){ | ||
pair<int,int> p = q.front(); | ||
q.pop(); | ||
for(int i =0;i<8;i++){ | ||
nextX = p.first+di[i]; | ||
nextY = p.second+dj[i]; | ||
if((nextX >= 1 && nextX<=n && nextY >=1 && nextY<=m) && matrice[nextX][nextY]==0){ | ||
matrice[nextX][nextY]=matrice[p.first][p.second]+1; | ||
q.push({nextX,nextY}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
int main(){ | ||
pair<int,int> posRomeo,posJulieta; | ||
f>>n>>m; | ||
char s[101]; | ||
f.get(); | ||
for(int i=1;i<=n;i++){ | ||
f.getline(s,101); | ||
for(int j =0;j<strlen(s);j++) | ||
{ | ||
switch(s[j]){ | ||
case 'R': | ||
posRomeo={i,j+1}; | ||
// a[i][j]=-101; | ||
break; | ||
case 'J': | ||
posJulieta={i,j+1}; | ||
// a[i][j]=-101; | ||
break; | ||
case 'X': | ||
a[i][j+1]=-1; | ||
break; | ||
} | ||
romeo[i][j+1]=julieta[i][j+1]=a[i][j+1]; | ||
} | ||
} | ||
|
||
fill(posRomeo,romeo); | ||
fill(posJulieta,julieta); | ||
|
||
// for(int i=1;i<=n;i++){ | ||
// for(int j=1;j<=m;j++){ | ||
// cout<<setw(3)<<julieta[i][j]<<" "; | ||
// } | ||
// cout<<endl; | ||
// } | ||
pair<int,int> res; | ||
int min = inf; | ||
for(int i=1;i<=n;i++){ | ||
for(int j=1;j<=m;j++){ | ||
if((romeo[i][j]==julieta[i][j] )&& (romeo[i][j]<min) && (romeo[i][j]>=1)){ | ||
res.first=i; | ||
res.second=j; | ||
min=romeo[i][j]; | ||
} | ||
} | ||
} | ||
g<<min<<" "<<res.first<<" "<<res.second; | ||
} |