0% found this document useful (0 votes)
41 views12 pages

Tema

The document contains code for solving several problems related to computational geometry and graph algorithms. The problems include razing a grid, balancing a scale, finding the greatest common divisor between nodes in a graph, simulating a robot's movements, finding the maximum area of a terrain within a limit, tracking marmots' positions, checking if soldier IDs form platoons, and rotating and reflecting a game board to match a target configuration. The code uses concepts like breadth-first search, greatest common divisor calculation, 2D arrays to represent grids/graphs, and matrix rotation/reflection.

Uploaded by

gorhds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views12 pages

Tema

The document contains code for solving several problems related to computational geometry and graph algorithms. The problems include razing a grid, balancing a scale, finding the greatest common divisor between nodes in a graph, simulating a robot's movements, finding the maximum area of a terrain within a limit, tracking marmots' positions, checking if soldier IDs form platoons, and rotating and reflecting a game board to match a target configuration. The code uses concepts like breadth-first search, greatest common divisor calculation, 2D arrays to represent grids/graphs, and matrix rotation/reflection.

Uploaded by

gorhds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

//raze

#include <fstream>

using namespace std;

ifstream cin("raze.in");
ofstream cout("raze.out");

int t,n,m,x,rs,cnt,a[140][140];
bool ok[4][140][140];
int main(){
cin >> t;
while(t--)
{
cin>>n>>m;
for(int i = 1; i <= n+3; i++)
{
for(int j = 1; j <= m+3; j++)
{
for(int k = 0; k < 4; k++)
ok[0][i][j] = 0;
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++) cin >> a[i][j];
}
for(int i = 2; i < n; i++)
{
for(int j = 2; j < m; j++)
{
if(a[i][j]) ok[0][i][j] = 1;
else ok[0][i][j] = ok[0][i-1][j-1];
}
}
for(int i = 2; i < n; i++)
{
for(int j = m-1; j > 1; j--)
{
if(a[i][j]) ok[1][i][j] = 1;
else ok[1][i][j] = ok[1][i-1][j+1];
}
}
for(int i = n-1; i > 1; i--)
{
for(int j = 2; j < m; j++)
{
if(a[i][j]) ok[2][i][j] = 1;
else ok[2][i][j] = ok[2][i+1][j-1];
}
}
for(int i = n-1; i > 1; i--)
{
for(int j = m-1; j > 1; j--)
{
if(a[i][j]) ok[3][i][j] = 1;
else ok[3][i][j] = ok[3][i+1][j+1];
}
}
for(int i = 2; i < n; i++)
{
for(int j = 2; j < m; j++)
{
x=0;
for(int k = 0; k < 4; k++) x += !ok[k][i][j];
if(x>rs){
rs = x;
cnt = 1;
}
else if(x==rs) cnt++;
}
}
cout<<rs<<' '<<cnt<<'\n';
}
return 0;
}

//balanta
#include <fstream>

using namespace std;

ifstream cin("balanta.in");
ofstream cout("balanta.out");

int mod, cntusoare, cntgrele ,b, a, c, x, v[1050], u[1050], grele[1050],


usoare[1050];
int main()
{
cin>>a>>b;
for (int i=1; i<=a; i++) grele[i] = usoare[i] = 1;

for (int t=1; t<=b; t++)


{
cin>>c;
for (int i=1; i<=a; i++)
{
v[i]=u[i]=0;
}
for (int i=1; i<=c; i++)
{
cin >> x;
v[x] = 1;
}
for (int i=1; i<=c; i++)
{
fin >> x;
u[x] = 1;
}

cin>>mod;

for (int i=1; i<=a;i++)


{
if (mod == 0)
{
if (v[i] || u[i]) usoare[i] = grele[i] = 0;
}
else if (mod==1)
{
if (v[i]==0) grele[i] = 0;
if (u[i]==0) usoare[i] = 0;
}
else if (mod==2)
{
if (v[i]==0) usoare[i] = 0;
if (u[i]==0) grele[i] = 0;
}
}
}
for (int i=1; i<=a; i++)
{
cntgrele+=grele[i];
cntusoare+= usoare[i];
}
if (cntgrele==0 && cntusoare==1)
{
for (int i=1; i<=a; i++)
{
if (usoare[i] != 0)
{
cout<<i;
return 0;
}
}
}
else if (cntgrele==1 && cntusoare==0)
{
for (int i=1; i<=a; i++)
{
if (grele[i] != 0)
{
fout<<i;
return 0;
}
}
}
cout<<0;
return 0;
}

//oz
#include <fstream>
using namespace std;

ifstream cin ("oz.in");


ofstream cout ("oz.out");

int n,m,a,b,r,d[100001],x[100001],y[100001],v[10001],ap,nr;
int main ()
{
cin>>n>>m;
for (int i=1;i<=n;i++) v[i] = 1;
for (int i=1;i<=m;i++)
{
cin>>x[i]>>y[i]>>d[i];
a = v[x[i]];
b = d[i];
while (b)
{
r = a%b;
a = b;
b = r;
}
v[x[i]] = v[x[i]]*d[i]/a;
a = v[y[i]];
b = d[i];
while (b)
{
r = a%b;
a = b;
b = r;
}
v[y[i]] = v[y[i]]*d[i]/a;

}
ap = 0;
for (int i=1;i<=m;i++)
{
a = v[x[i]];
b = v[y[i]];
while (b)
{
r = a%b;
a = b;
b = r;
}
if (a != d[i]) ap++;
}
for (i=1;i<=n;i++)
{
if (v[i] > 2000000000) ap++;
}
if (ap == 0)
{
for (i=1;i<=n;i++) cout<<v[i]<<" ";
}
else cout<<-1;
return 0;
}

//robot3
#include <fstream>
using namespace std;
ifstream cin("robot3.in");
ofstream cout("robot3.out");
int n,i,j,q,is,js,en;
int v[1010][1010],m[5010];
char c;
int main(){
cin>>n>>q>>is>>js;

for (i=1;i<=q;i++) {
cin>>c;
if (c=='N') {
m[i]=1;
}
if (c=='E') {
m[i]=2;
}
if (c=='S') {
m[i]=3;
}
if (c=='V') {
m[i]=4;
}
}
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
cin>>v[i][j];
}
}
en=1;
for (i=1;i<=q;i++) {
en+=v[is][js];
en--;
if (en==0) {
cout<<is<<" "<<js;
return 0;
}
if (m[i]==1) {
is--;
}
if (m[i]==2) {
js++;
}
if (m[i]==3) {
is++;
}
if (m[i]==4) {
js--;
}
}
cout<<is<<" "<<js;
}

//teren
include <fstream>

using namespace std;


ifstream cin("teren.in");
ofstream cout("teren.out");
int n,m,k,v[305][305],a[305];
void citire()
{
int i,j,x;
cin>>n>>m>>k;
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
cin>>x;
v[i][j]=v[i-1][j]+x;
}
}
}
void afisare(int vec[])
{
int i;
for(i=1; i<=m; i++)
cout<<vec[i]<<" ";
cout<<endl;
}
void rezolvare()
{
int i,j,d,st,dr,sum,arie_max=0;
{
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
for(d=1; d<=m; d++) a[d]=v[j][d]-v[i-1][d];
st=1;
sum=0;
for(dr=1; dr<=m; dr++)
{
sum+=a[dr];
while(st<=dr && sum>k)
{
sum-=a[st];
st++;
}
if(st<=dr)
arie_max=max(arie_max,(j-i+1)*(dr-st+1));
}
}
}
}
cout<<arie_max;
}
int main()
{
citire();
rerezolva();
return 0;
}

//marmote
#include <fstream>
#include <cmath>

using namespace std;

ifstream cin("marmote.in");
ofstream cout("marmote.out");
int N, M, K, L, x[100000], y[100000],rez,k, X, Y;
int main()
{
cin>>N>>M>>K>>L;
rez=1;
k=1;
cin>>x[0]>>y[0];
cout<<1<<endl;
for (int i=1;i<K;++i) {
bool ok = true;
cin>>X>>Y;
for (int j=0;j<k;++j)
if (abs(X-x[j])+abs(Y-y[j])<=L*2)
ok=false;

if (ok == true)
{
++rez;
x[k] = X;
y[k] = Y;
++k;
cout<<i+1<<endl;
}
}
return 0;
//pluton
#include <fstream>

using namespace std;

ifstream fin("pluton.in");
ofstream fout("pluton.out");

bool pluton(int n, int p)


{
int w=p;
bool egale;
while (n)
{
int uc=n%10;
egale=false;
while(w)
{
int uc2=w%10;
if (uc==uc2) {
egale=true;
}
w/=10;
}
if (egale==false) return false;
w=p;
n/=10;
}
if (egale==true) return true;
}
long long v[4001];
int n,poz,soldatii,maxim,aparitii,plutoane;
int main()
{
fin>>n;
maxim=0;
for (int i=1;i<=n;i++)
{
fin>>v[i];
}
poz=1;
for (int i=1;i<=n;i++)
{
soldatii=1;
while (poz<n)
{
if (pluton(v[i], v[poz]))
{
soldatii++;
}
poz++;
}
plutoane++;
if (soldatii>maxim)
{
maxim=soldatii;
aparitii=1;
}
if (soldatii==maxim) aparitii++;
}
fout<<plutoane<<endl<<maxim<<endl<<aparitii;
return 0;
}

//cartele
#include <algorithm>
#include <fstream>
using namespace std;

ifstream cin("cartele.in");
ofstream cout("cartele.out");

int cartela[55][55];
int aux[55][55];
int M[55][55];
int n;

void rot90()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++) aux[j][n - i + 1] = cartela[i][j];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cartela[i][j] = aux[i][j];
}
}
bool verif()
{
for(int i=1;i<=n;i++)
{
for(int j=1; j<=n;j++)
{
if(M[i][j] != cartela[i][j])
return false;
}
}
return true;
}

void rezolva()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin >> cartela[i][j];
}
for(int i=1;i<=4;i++)
{
if(verif() == 1)
{
cout<<1<<endl;
return;
}
rot90();
}
for(int i=1;i<=n;i++) reverse(cartela[i] + 1, cartela[i] + n + 1);

for(int i=1;i<=4;i++)
{
if(verif() == 1)
{
cout<<1<< endl;
return;
}
rot90();
}
cout<<0<<endl;
return;
}
int c;
int main()
{

cin>>n>>c;
for(int i=1;i<=n;i++)
{
for(int j=1; j<=n;j++)
{
cin >> M[i][j];
}
}

for(int i=1;i<=c;i++)
rezolva();
return 0;
}

//amat
#include <fstream>

using namespace std;

ifstream fin("amat.in");
ofstream fout("amat.out");

int grid[1005][1005];
bool ok[1005][1005];
int m, n;
int c;
int c1 = 0, l1 = 0;
int c2 = 0, l2 = 0;
int maxxl = -10;
int maxxc = -10;

bool verify()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (ok[i][j] == 0)
{
return 0;
}
}
}
return 1;
}

int main()
{
fin >> c;
fin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
fin >> grid[i][j];
}
}
if (c == 1)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
int x1, y1, x2, y2;
if (ok[i][j] == 0)
{
x1 = x2 = i;
y1 = y2 = j;
int g = i, f = j;
for (g; g <= n && grid[g][f] == grid[i][j]; g++)
{
for (f; f <= m && grid[g][f] == grid[i][j]; f++)
{
ok[g][f] = 1;
x2 = g;
y2 = f;
}
f = j;
}
int arie1 = (c2 - c1 + 1) * (l2 - l1 + 1), arie2 = (x2 - x1 +
1) * (y2 - y1 + 1);
if (arie1 < arie2)
{
c1 = x1;
c2 = x2;
l1 = y1;
l2 = y2;
maxxl = i;
maxxc = j;
}
else if (arie1 == arie2 && i > maxxl)
{
c1 = x1;
c2 = x2;
l1 = y1;
l2 = y2;
maxxl = i;
maxxc = j;
}
else if (arie1 == arie2 && i == maxxl && j > maxxc)
{
c1 = x1;
c2 = x2;
l1 = y1;
l2 = y2;
maxxl = i;
maxxc = j;
}
}
}
}
fout << c1 << ' ' << l1 << ' ' << c2 << ' ' << l2;
}
else
{
int cnt = 0;
int q, k;
fin >> q >> k;
int x1, y1, x2, y2, nr;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (grid[i][j] >= k)
{
ok[i][j] = 1;
}
}
}
for (int i = 1; i <= q; i++)
{
fin >> x1 >> y1 >> x2 >> y2 >> nr;
for (int a = x1; a <= x2; a++)
{
for (int b = y1; b <= y2; b++)
{
if (ok[a][b] == 0)
{
if (grid[a][b] + nr >= k)
{
ok[a][b] = 1;
}
else
{
grid[a][b] += nr;
}
}
}
}
cnt++;
bool ok = verify();
if (ok)
{
i = q + 1;
}
}
fout << cnt;
}

You might also like