Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.99 KB | None | 0 0
  1. Se citeşte un număr natural nenul n. Să se afişeze, în ordine lexicografică, permutările mulţimii {1,2,..,n}.
  2.  
  3. #include <fstream>
  4.  
  5. using namespace std;
  6. ifstream fin("permutari.in");
  7. ofstream fout("permutari.out");
  8. int st[101],n;
  9. bool valid(int k)
  10. {
  11. for(int i=1;i<k;i++)
  12. if(st[i]==st[k])
  13. return 0;
  14. return 1;
  15. }
  16. void tipar(int k)
  17. {
  18. for(int i=1;i<=k;i++)
  19. fout<<st[i]<<" ";
  20. fout<<endl;
  21. }
  22. void bkt()
  23. {
  24. int k,ok;
  25. k=1;
  26. st[k]=0;
  27. while(k>0)
  28. {
  29. ok=0;
  30. while(st[k]<n&&!ok)
  31. {
  32. st[k]++;
  33. ok=valid(k);
  34. }
  35. if(ok==1)
  36. {
  37. if(k==n)
  38. tipar(k);
  39. else
  40. st[++k]=0;
  41. }
  42. else
  43. k--;
  44. }
  45. }
  46. int main()
  47. {
  48. fin>>n;
  49. bkt();
  50. return 0;
  51. }
  52.  
  53.  
  54. Se citeşte un număr natural nenul n. Să se afişeze, în ordine invers lexicografică, permutările mulţimii {1,2,..,n}.
  55. #include <fstream>
  56.  
  57. using namespace std;
  58. ifstream fin("permutari1.in");
  59. ofstream fout("permutari1.out");
  60. int st[101],n;
  61. bool valid(int k)
  62. {
  63. for(int i=1;i<k;i++)
  64. if(st[i]==st[k])
  65. return 0;
  66. return 1;
  67. }
  68. void tipar(int k)
  69. {
  70. for(int i=1;i<=k;i++)
  71. fout<<st[i]<<" ";
  72. fout<<endl;
  73. }
  74. void bkt()
  75. {
  76. int k,ok;
  77. k=1;
  78. st[k]=n+1;
  79. while(k>0)
  80. {
  81. ok=0;
  82. while(st[k]>1&&ok==0)
  83. {
  84. st[k]--;
  85. ok=valid(k);
  86. }
  87. if(ok==1)
  88. {
  89. if(k==n)
  90. tipar(k);
  91. else
  92. st[++k]=n+1;
  93. }
  94. else
  95. k--;
  96. }
  97. }
  98. int main()
  99. {
  100. fin>>n;
  101. bkt();
  102. return 0;
  103. }
  104.  
  105.  
  106. Se citeşte un număr natural nenul n, apoi n numere naturale distincte. Să se afişeze, în ordine lexicografică, permutările mulţimii formate din cele n numere citite.
  107.  
  108. #include <fstream>
  109. #include <algorithm>
  110. using namespace std;
  111. ifstream fin("permutari2.in");
  112. ofstream fout("permutari2.out");
  113. int st[101],n, v[101];
  114. bool valid(int k)
  115. {
  116. for(int i=1;i<k;i++)
  117. if(st[i]==st[k])
  118. return 0;
  119. return 1;
  120. }
  121. void tipar(int k)
  122. {
  123. for(int i=1;i<=k;i++)
  124. fout<<v[st[i]]<<" ";
  125. fout<<endl;
  126. }
  127. void bkt()
  128. {
  129. int k,ok;
  130. k=1;
  131. st[k]=0;
  132. while(k>0)
  133. {
  134. ok=0;
  135. while(st[k]<n&&!ok)
  136. {
  137. st[k]++;
  138. ok=valid(k);
  139. }
  140. if(ok==1)
  141. {
  142. if(k==n)
  143. tipar(k);
  144. else
  145. st[++k]=0;
  146. }
  147. else
  148. k--;
  149. }
  150. }
  151. int main()
  152. {
  153. fin>>n;
  154. for(int i=1;i<=n;i++)
  155. fin>>v[i];
  156. sort(v,v+n+1);
  157. bkt();
  158. return 0;
  159. }
  160.  
  161.  
  162. Se consideră o tablă de șah de dimensiune n. Să se plaseze pe tablă n regine astfel încât să nu existe două regine care să se atace.
  163.  
  164. #include <iostream>
  165. #include <cmath>
  166.  
  167. using namespace std;
  168.  
  169. int n,x[20];
  170. bool gasit;
  171.  
  172. bool cont(int k)
  173. {
  174. for(int i=1;i<k;i++)
  175. {
  176. if(x[i]==x[k])
  177. return false ;
  178. if(k-i==abs(x[k]-x[i]))
  179. return false;
  180. }
  181. return true;
  182. }
  183.  
  184. void afisare(int n)
  185. {
  186. gasit=true;
  187. for(int i=1;i<=n;i++)
  188. {
  189. for(int j=1;j<=n;j++)
  190. if(x[j]==i)
  191. cout<<"*";
  192. else
  193. cout<<"-";
  194. cout<<'\n';
  195. }
  196. }
  197.  
  198.  
  199. void bkt(int k)
  200. {
  201. for(int i=1;!gasit && i<=n;i++)
  202. {
  203. x[k]=i;
  204. if(cont(k))
  205. if(k==n)
  206. afisare(n);
  207. else
  208. bkt(k+1);
  209. }
  210. }
  211. int main()
  212. {
  213. cin>>n;
  214. bkt(1);
  215. return 0;
  216. }
  217.  
  218. Fie mulţimea M={1,2,..,n} şi P(1),P(2),...,P(n) o permutare a ei. Elementul x din M se numeşte punct fix dacă P(x)=x.
  219. Cerinţa
  220.  
  221. Se citeşte un număr natural nenul n. Să se afişeze, în ordine lexicografică, permutările fără puncte fixe ale mulţimii {1,2,..,n}.
  222.  
  223. #include <fstream>
  224. using namespace std;
  225.  
  226. ifstream f("permpf.in");
  227. ofstream g("permpf.out");
  228.  
  229. int st[100],n;
  230. bool valid(int k)
  231. {
  232. for(int i=1;i<k;i++)
  233. if(st[i]==st[k]||st[i]==i||st[k]==k)
  234. return 0;
  235. return 1;
  236. }
  237.  
  238. void tipar(int k)
  239. {
  240. for(int i=1;i<=k;i++)
  241. g<<st[i]<<' ';
  242. g<<'\n';
  243. }
  244.  
  245. void bkt()
  246. {
  247. int k=1;
  248. st[k]=0;
  249. while(k>0)
  250. {
  251. bool ok=0;
  252. while(st[k]<n&&!ok)
  253. {
  254. st[k]++;
  255. ok=valid(k);
  256. }
  257. if(ok)
  258. if(n==k)
  259. tipar(k);
  260. else
  261. st[++k]=0;
  262. else
  263. k--;
  264.  
  265. }
  266. }
  267. int main()
  268. {
  269. f>>n;
  270. bkt();
  271. return 0;
  272. }
  273.  
  274. Se citeşte un număr natural nenul n, apoi n numere naturale distincte. Să se afişeze, în ordine lexicografică, șirurile din cele n valori cu proprietatea că oricare două valori învecinate sunt prime între ele.
  275.  
  276. #include <fstream>
  277. #include <algorithm>
  278. using namespace std;
  279. ifstream fin("sirpie.in");
  280. ofstream fout("sirpie.out");
  281. int st[101],n, v[101];
  282. int cmmdc(int a,int b)
  283. {
  284. int t;
  285. while(b!=0)
  286. {
  287. t=b;
  288. b=a%b;
  289. a=t;
  290. }
  291. return a;
  292. }
  293. bool valid(int k)
  294. {
  295. for(int i=1;i<k;i++)
  296. if(st[i]==st[k])
  297. return 0;
  298. if(k>1)
  299. {
  300. if(cmmdc(v[st[k]],v[st[k-1]])==1)
  301. return 1;
  302. else
  303. return 0;
  304. }
  305. return 1;
  306. }
  307. void tipar(int k)
  308. {
  309. for(int i=1;i<=k;i++)
  310. fout<<v[st[i]]<<" ";
  311. fout<<endl;
  312. }
  313. void bkt()
  314. {
  315. int k,ok;
  316. k=1;
  317. st[k]=0;
  318. while(k>0)
  319. {
  320. ok=0;
  321. while(st[k]<n&&!ok)
  322. {
  323. st[k]++;
  324. ok=valid(k);
  325. }
  326. if(ok==1)
  327. {
  328. if(k==n)
  329. tipar(k);
  330. else
  331. st[++k]=0;
  332. }
  333. else
  334. k--;
  335. }
  336. }
  337. int main()
  338. {
  339. fin>>n;
  340. for(int i=1;i<=n;i++)
  341. fin>>v[i];
  342. sort(v,v+n+1);
  343. bkt();
  344. return 0;
  345. }
  346.  
  347.  
  348. Se numește anagramă a unui cuvânt dat, un alt cuvânt ce conține toate literele primului, eventual în altă ordine.
  349. Cerinţa
  350.  
  351. Se dă un cuvânt din cel mult 8 litere distincte. Să se afișeze, în ordine alfabetică, toate anagramele acestui cuvânt.
  352.  
  353. #include <fstream>
  354. #include <algorithm>
  355. #include <string.h>
  356. using namespace std;
  357. ifstream fin("anagrame1.in");
  358. ofstream fout("anagrame1.out");
  359. int st[101],n;
  360. char v[9];
  361. bool valid(int k)
  362. {
  363. for(int i=1;i<k;i++)
  364. if(st[i]==st[k])
  365. return 0;
  366. return 1;
  367. }
  368. void tipar(int k)
  369. {
  370. for(int i=1;i<=k;i++)
  371. fout<<v[st[i]-1];
  372. fout<<endl;
  373. }
  374. void bkt()
  375. {
  376. int k,ok;
  377. k=1;
  378. st[k]=0;
  379. while(k>0)
  380. {
  381. ok=0;
  382. while(st[k]<n&&!ok)
  383. {
  384. st[k]++;
  385. ok=valid(k);
  386. }
  387. if(ok==1)
  388. {
  389. if(k==n)
  390. tipar(k);
  391. else
  392. st[++k]=0;
  393. }
  394. else
  395. k--;
  396. }
  397. }
  398. int main()
  399. {
  400. fin>>v;
  401. n=strlen(v);
  402. sort(v,v+n);
  403. bkt();
  404. return 0;
  405. }
  406.  
  407.  
  408. Se citește un număr natural n (n<16). Afișați în ordine lexicografică toate permutările mulțimii {1,2,…,n} în care elementele pare sunt puncte fixe (nu își schimbă poziția).
  409.  
  410. #include <iostream>
  411.  
  412. using namespace std;
  413.  
  414. int st[20],n;
  415. void tipar()
  416. {
  417. for(int i=1; i<=n; i++)
  418. cout<<st[i]<<" ";
  419. cout<<'\n';
  420. }
  421. bool valid(int k)
  422. {
  423. if (k%2==0&&st[k]!=k)
  424. return 0;
  425. for(int i=1; i<k; i++)
  426. if(st[i]==st[k])
  427. return 0;
  428. return 1;
  429.  
  430. }
  431. void bkt(int k)
  432. {
  433. for (int i=1; i<=n; i++)
  434. {
  435. st[k]=i;
  436. if (valid(k))
  437. if (k==n)
  438. tipar();
  439. else
  440. bkt(k+1);
  441.  
  442. }
  443. }
  444. int main()
  445. {
  446. cin>>n;
  447. bkt(1);
  448. return 0;
  449. }
  450.  
  451. Se citesc două numere naturale nenule n și k. Să se afişeze, în ordine lexicografică, aranjamentele de câte k elemente ale mulţimii {1,2,..,n}.
  452.  
  453. #include <fstream>
  454.  
  455. using namespace std;
  456.  
  457. ifstream fin("aranjamente.in");
  458. ofstream fout("aranjamente.out");
  459.  
  460. int sol[100],n,p;
  461.  
  462. bool valid(int k)
  463. {
  464. for(int i=1;i<k;i++)
  465. if(sol[i]==sol[k])
  466. return 0;
  467. return 1;
  468. }
  469.  
  470. void tipar(int k)
  471. {
  472. for(int i=1;i<=k;i++)
  473. fout<<sol[i]<<" ";
  474. fout<<'\n';
  475. }
  476.  
  477.  
  478. void bktr(int k)
  479. {
  480. for(int i=1;i<=n;i++)
  481. {
  482. sol[k]=i;
  483. if(valid(k))
  484. if(k==p)
  485. tipar(k);
  486. else
  487. bktr(k+1);
  488. }
  489. }
  490.  
  491. int main()
  492. {
  493. fin>>n>>p;
  494. bktr(1);
  495. return 0;
  496. }
  497.  
  498. Se citesc două numere naturale nenule n și k. Să se afişeze, în ordine lexicografică, submulțimile de câte k elemente ale mulţimii {1,2,..,n}.
  499.  
  500. #include <fstream>
  501.  
  502. using namespace std;
  503. ifstream fin("combinari.in");
  504. ofstream fout("combinari.out");
  505. int n, x[100],p;
  506.  
  507. void tipar(int k){
  508. for(int i=1 ; i<=k ; ++i)
  509. fout << x[i] << " ";
  510. fout << endl;
  511. }
  512. bool valid(int k){
  513. for(int i=1;i<k;i++)
  514. if(x[i]==x[k])
  515. return false;
  516. if(k == 1)
  517. return true;
  518. if(x[k] > x[k-1])
  519. return true;
  520. return false;
  521. }
  522. void back(int k){
  523. for(int i=1;i<=n;++i)
  524. {
  525. x[k]=i;
  526. if(valid(k))
  527. {
  528. if(k==p)
  529. tipar(k);
  530. else
  531. back(k+1);
  532. }
  533. }
  534.  
  535. }
  536.  
  537.  
  538. int main()
  539. {
  540. fin>>n>>p;
  541. back(1);
  542. return 0;
  543. }
  544.  
  545. Se citește un număr natural nenul n. Să se afişeze, în ordine lexicografică, toate submulțimile nevide ale mulţimii {1,2,..,n}.
  546.  
  547. #include <fstream>
  548.  
  549. using namespace std;
  550. ifstream fin("submultimi.in");
  551. ofstream fout("submultimi.out");
  552. int n, x[100];
  553.  
  554. void afis(int k){
  555. for(int i=1 ; i<=k ; ++i)
  556. fout << x[i] << " ";
  557. fout << endl;
  558. }
  559. bool valid(int k){
  560. if(k == 1)
  561. return true;
  562. if(x[k] > x[k-1])
  563. return true;
  564. return false;
  565. }
  566. void back(int k){
  567. for(int i=1;i<=n;++i)
  568. {
  569. x[k]=i;
  570. if(valid(k))
  571. {
  572. afis(k);
  573. back(k+1);
  574. }
  575. }
  576. }
  577.  
  578.  
  579. int main()
  580. {
  581. fin>>n;
  582. back(1);
  583. return 0;
  584. }
  585.  
  586. Se dă o tablă dreptunghiulară formată din n linii și m coloane, definind n*m zone, unele dintre ele fiind libere, altele conținând obstacole. În zona aflată la poziția is, js se află un șoarece care se poate deplasa pe tablă trecând din zona curentă în zona învecinată cu aceasta pe linie sau pe coloană. Scopul sau este să ajungă la o bucată de brânză aflată în zona de la poziția ib, jb, fără a părăsi tabla, fără a trece prin zone care conțin obstacole și fără a trece de două ori prin aceeași zonă.
  587.  
  588. Determinați câte modalități prin care șoarecele poate ajunge de la poziția inițială la cea a bucății de brânză există.
  589. #include <fstream>
  590.  
  591. using namespace std;
  592. ifstream fin("soarece.in");
  593. ofstream fout("soarece.out");
  594. int n,m,L[100][100],xs,ys,xb,yb,nrsol;
  595. int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
  596. void citire()
  597. {
  598. fin>>n>>m;
  599. for(int i=1;i<=n;i++)
  600. for(int j=1;j<=m;j++)
  601. fin>>L[i][j];
  602. fin>>xs>>ys>>xb>>yb;
  603. }
  604. void afisare()
  605. {
  606. for(int i=1;i<=n;i++)
  607. {
  608. for(int j=1;j<=m;j++)
  609. if(L[i][j]==2)
  610. fout<<"* ";
  611. else
  612. fout<<L[i][j]<<" ";
  613. fout<<"\n";
  614. }
  615. fout<<'\n';
  616. }
  617. void bktPlan(int x,int y)
  618. {
  619. int xv,yv;
  620. for(int i=0;i<4;i++)
  621. {
  622. xv=x+dx[i];
  623. yv=y+dy[i];
  624. if(xv>0&&xv<=n&&yv>0&&yv<=m&&L[xv][yv]==0)
  625. {
  626. L[x][y]=2;
  627. if(xv==xb&&yv==yb)
  628. {
  629. nrsol++;
  630. //afisare();
  631. }
  632. else
  633. bktPlan(xv,yv);
  634. L[x][y]=0;
  635. }
  636. }
  637. }
  638. int main()
  639. {
  640. citire();
  641. bktPlan(xs,ys);
  642. fout<<nrsol;
  643. return 0;
  644. }
  645.  
  646.  
  647. Se dă o tablă de șah formată din n linii și m coloane, definind n*m zone, unele dintre ele fiind libere, altele conținând obstacole. În zona de coordonate 1 1 se află un cal care se poate deplasa pe tablă în L, ca la șah, fără a părăsi tabla, fără a trece prin zone care conțin obstacole și fără a trece de două ori prin aceeași zonă.
  648.  
  649. Determinați o modalitate prin care calul poate ajunge în zona de coordonate n m – unde se află o căpiță de fân.
  650.  
  651. #include <fstream>
  652.  
  653. using namespace std;
  654. int n,m,L[100][100],xs,ys,xb,yb,nrsol;
  655. int dx[]={-2,-2,-1,1,2,2,1,-1},dy[]={-1,1,2,2,1,-1,-2,-2};
  656.  
  657. ifstream fin ("traseucal.in");
  658. ofstream fout ("traseucal.out");
  659.  
  660. void citire()
  661. {
  662. fin>>n>>m;
  663. for (int i=1; i<=n; i++)
  664. for (int j=1; j<=m; j++)
  665. {
  666. fin>>L[i][j];
  667. L[i][j]=-L[i][j];
  668. }
  669. xs=ys=1;
  670. xb=n;
  671. yb=m;
  672. }
  673.  
  674. void afisare()
  675. {
  676. for(int i=1;i<=n;i++)
  677. {
  678. for(int j=1;j<=m;j++)
  679. if (L[i][j]==-1)
  680. fout<<"0 ";
  681. else
  682. fout<<L[i][j]<<" ";
  683. fout<<endl;
  684. }
  685. }
  686. void bktPlan(int x,int y,int k)
  687. {
  688. L[x][y]=k;
  689. if (x==xb && y==yb)
  690. {
  691. afisare();
  692. nrsol++;
  693. }
  694. else
  695. for(int i=0;i<8;i++)
  696. {
  697. int xv=x+dx[i];
  698. int yv=y+dy[i];
  699. if(xv>0 && xv<=n && yv>0 && yv<=m && L[xv][yv]==0 && nrsol==0)
  700. bktPlan(xv,yv,k+1);
  701. }
  702. L[x][y]=0;
  703. }
  704. int main()
  705. {
  706. citire();
  707. bktPlan(xs,ys,1);
  708. return 0;
  709. }
  710.  
  711. Se consideră o tablă de joc de formă dreptunghiulară, împărţită în n lini şi m coloane. Se obţin astfel n*m zone şi se cunoaște înălțimea fiecărei zone. La o poziție cunoscută – linia istart, coloana jstart se află o bilă care se poate deplasa pe o poziție vecină (sus, jos, stânga, dreapta) doar dacă înălțimea poziției vecine este strict mai mică decât înălțimea poziției curente.
  712.  
  713. Determinați numărul maxim de zone prin care poate să treacă bila pentru a ajunge pe una dintre marginile tablei de joc.
  714.  
  715. #include <fstream>
  716.  
  717. using namespace std;
  718.  
  719. ifstream fin("bila.in");
  720. ofstream fout("bila.out");
  721.  
  722. int xi,xj,n,m;
  723. int a[25][25];
  724. int pasi,maxim;
  725.  
  726. void citire()
  727. {
  728. fin>>n>>m;
  729. for(int i=1;i<=n;i++)
  730. for(int j=1;j<=m;j++)
  731. fin>>a[i][j];
  732. fin>>xi>>xj;
  733. }
  734. void backPlan(int i,int j,int pasi)
  735. {
  736. if(i==n || i==1 || j==m || j==1)
  737. {
  738. if(pasi>maxim)
  739. maxim=pasi;
  740. }
  741. else
  742. {
  743. if(a[i+1][j]<a[i][j])
  744. backPlan(i+1,j,pasi+1);
  745. if(a[i-1][j]<a[i][j])
  746. backPlan(i-1,j,pasi+1);
  747. if(a[i][j+1]<a[i][j])
  748. backPlan(i,j+1,pasi+1);
  749. if(a[i][j-1]<a[i][j])
  750. backPlan(i,j-1,pasi+1);
  751. }
  752. }
  753.  
  754. int main()
  755. {
  756. citire();
  757. backPlan(xi,xj,1);
  758. fout<<maxim;
  759. return 0;
  760. }
  761.  
  762. Hercule trebuie sa strabată un labirint cu capcane reprezentat de o matrice cu n linii și m coloane. Pentru fiecare celula a labirintului, se cunoaște timpul exprimat în minute după care celula respectivă devine capcană. După ce o celula devine capcana, Hercule piere dacă intră în acea celulă. Initial Hercule se află în celula de coordonate (1, 1) și trebuie să ajungă în celula de cordonate (n,m).
  763.  
  764. Sa se afișeze numarul total de drumuri pe care le poate urma Hercule prin labirint, astfel încât Hercule să nu piară
  765.  
  766. #include <iostream>
  767. #include <fstream>
  768. using namespace std;
  769. ifstream fin("hercule.in");
  770. ofstream fout("hercule.out");
  771.  
  772. int n,m, L[15][15],aux[15][15],nrsol;
  773. int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
  774. void citire()
  775. {
  776. fin>>n>>m;
  777. for (int i=1; i<=n; i++)
  778. for(int j=1; j<=m; j++)
  779. fin>>L[i][j];
  780. }
  781. void bordare()
  782. {
  783. for (int i=0; i<=n+1; i++)
  784. L[i][0]=L[i][m+1]=0;
  785. for(int j=0;j<=m+1;j++)
  786. L[0][j]=L[n+1][j]=0;
  787. }
  788. void bktplan(int x,int y,int pas)
  789. {
  790. if(x==n&&y==m)
  791. nrsol++;
  792. else
  793. {
  794. aux[x][y]=1;
  795. for(int i=0;i<4;i++)
  796. {
  797. int xv=x+dx[i];
  798. int yv=y+dy[i];
  799. if(aux[xv][yv]==0&&L[xv][yv]-pas>=1)
  800. bktplan(xv,yv,pas+1);
  801. }
  802. aux[x][y]=0;
  803. }
  804. }
  805. int main()
  806. {
  807. citire();
  808. bordare();
  809. bktplan(1,1,1);
  810. fout<<nrsol;
  811. return 0;
  812. }
  813.  
  814. sudoxu
  815.  
  816. #include <fstream>
  817. using namespace std;
  818. ifstream f("sudoku.in");
  819. ofstream g("sudoku.out");
  820. int a[10][10],i,j,n,k;//k=nivelul curent in stiva, n=inaltimea stivei
  821. //in stiva punem indicii elementele din matrice care sunt zero (necompletate)
  822. int st[3][100],as,ev,gasit;
  823. //as=are succesor, ev=nivelul k e corect completat (valid)
  824. void succesor()
  825. {
  826. if(a[st[1][k]][st[2][k]]<9)
  827. {
  828. a[st[1][k]][st[2][k]]++;
  829. as=1;
  830. }
  831. else as=0;
  832. }
  833. void valid()
  834. {
  835. int i,j;
  836. ev=1;
  837. //parcurg linia
  838. for(j=1;j<=9;j++)
  839. if(j!=st[2][k]&&a[st[1][k]][j]==a[st[1][k]][st[2][k]])
  840. ev=0;
  841. //parcurg coloana elementului de pe nivelul k
  842. for(i=1;i<=9;i++)
  843. if(i!=st[1][k]&&a[i][st[2][k]]==a[st[1][k]][st[2][k]])
  844. ev=0;
  845. //parcurg careul
  846. if(st[1][k]>=1&&st[1][k]<=3&&st[2][k]>=1&&st[2][k]<=3)
  847. {
  848. for(i=1;i<=3;i++)
  849. for(j=1;j<=3;j++)
  850. if(a[st[1][k]][st[2][k]]==a[i][j]&&(i!=st[1][k]||j!=st[2][k]))
  851. ev=0;
  852. }
  853. else
  854. if(st[1][k]>=1&&st[1][k]<=3&&st[2][k]>=4&&st[2][k]<=6)
  855. {for(i=1;i<=3;i++)
  856. for(j=4;j<=6;j++)
  857. if(a[st[1][k]][st[2][k]]==a[i][j]&&(i!=st[1][k]||j!=st[2][k]))
  858. ev=0;}
  859. else
  860. if(st[1][k]>=1&&st[1][k]<=3&&st[2][k]>=7&&st[2][k]<=9)
  861. {for(i=1;i<=3;i++)
  862. for(j=7;j<=9;j++)
  863. if(a[st[1][k]][st[2][k]]==a[i][j]&&(i!=st[1][k]||j!=st[2][k]))
  864. ev=0;}
  865. else
  866. if(st[1][k]>=4&&st[1][k]<=6&&st[2][k]>=1&&st[2][k]<=3)
  867. {for(i=4;i<=6;i++)
  868. for(j=1;j<=3;j++)
  869. if(a[st[1][k]][st[2][k]]==a[i][j]&&(i!=st[1][k]||j!=st[2][k]))
  870. ev=0;}
  871. else
  872. if(st[1][k]>=4&&st[1][k]<=6&&st[2][k]>=4&&st[2][k]<=6)
  873. {for(i=4;i<=6;i++)
  874. for(j=4;j<=6;j++)
  875. if(a[st[1][k]][st[2][k]]==a[i][j]&&(i!=st[1][k]||j!=st[2][k]))
  876. ev=0;}
  877. else
  878. if(st[1][k]>=4&&st[1][k]<=6&&st[2][k]>=7&&st[2][k]<=9)
  879. {for(i=4;i<=6;i++)
  880. for(j=7;j<=9;j++)
  881. if(a[st[1][k]][st[2][k]]==a[i][j]&&(i!=st[1][k]||j!=st[2][k]))
  882. ev=0;}
  883. else
  884. if(st[1][k]>=7&&st[1][k]<=9&&st[2][k]>=1&&st[2][k]<=3)
  885. {for(i=7;i<=9;i++)
  886. for(j=1;j<=3;j++)
  887. if(a[st[1][k]][st[2][k]]==a[i][j]&&(i!=st[1][k]||j!=st[2][k]))
  888. ev=0;}
  889. else
  890. if(st[1][k]>=7&&st[1][k]<=9&&st[2][k]>=4&&st[2][k]<=6)
  891. {for(i=7;i<=9;i++)
  892. for(j=4;j<=6;j++)
  893. if(a[st[1][k]][st[2][k]]==a[i][j]&&(i!=st[1][k]||j!=st[2][k]))
  894. ev=0;}
  895. else
  896. if(st[1][k]>=7&&st[1][k]<=9&&st[2][k]>=7&&st[2][k]<=9)
  897. {for(i=7;i<=9;i++)
  898. for(j=7;j<=9;j++)
  899. if(a[st[1][k]][st[2][k]]==a[i][j]&&(i!=st[1][k]||j!=st[2][k]))
  900. ev=0;}
  901. }
  902. void tipar()
  903. {
  904. int i,j;
  905. for(i=1;i<=9;i++)
  906. {
  907. for(j=1;j<=9;j++)
  908. g<<a[i][j]<<" ";
  909. g<<endl;
  910. }
  911. gasit=1;
  912. }
  913. int main()
  914. {
  915. for(i=1;i<=9;i++)
  916. for(j=1;j<=9;j++)
  917. {
  918. f>>a[i][j];
  919. if(a[i][j]==0)
  920. {
  921. n++;
  922. st[1][n]=i; //linia
  923. st[2][n]=j; //coloana
  924. }
  925. }
  926. k=1;
  927. while(k>0&&!gasit)
  928. {
  929. do
  930. {
  931. succesor();
  932. if(as)
  933. valid();
  934. }while(as&&!ev);
  935. if(as)
  936. if(k==n)
  937. tipar();
  938. else
  939. k++;
  940. else {a[st[1][k]][st[2][k]]=0;k--;}
  941. }
  942. return 0;
  943. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement